盒子
盒子
文章目录
  1. 浏览器支持
  2. 在ie8下支持canvas

use-the-html5-canvas-in-ie8

在主流浏览器中,canvas标签使用如下:

<!DOCTYPE html>
<html>
<body>

<p>Image to use:</p>
<img id="scream" src="/images/avatar.jpg" alt="The Scream" width="220" height="277">

<p>Canvas to fill:</p>
<canvas id="myCanvas" width="250" height="300"
style="border:1px solid #d3d3d3;">

Your browser does not support the HTML5 canvas tag.</canvas>

<p><button onclick="myCanvas()">Try it</button></p>

<script>
function myCanvas() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img,10,10);
}
</script>


</body>
</html>

浏览器支持


















Element
<canvas> 4.0 9.0 2.0 3.1 9.0

在ie8下支持canvas

1.引入html5.jsexcanvas.js

<head>
<!--[if lt IE 9]>
<script type="text/javascript" src="excanvas.js"></script>
<script type="text/javascript" src="html5.js"></script>
<![endif]-->

</head>

2.ie8不支持getContext方法
调用excanvas中的GvmlCanvasManager.initElement方法,由于最后的

G_vmlCanvasManager = G_vmlCanvasManager_;

使得G_vmlCanvasManager的作用域是当前window,所以在需要初始化Canvas的时候可以采用如下的方法:

var c = document.getElementById("myCanvas");
if (c.getContext===undefined){
c = window.G_vmlCanvasManager.initElement(c);
}
var ctx = c.getContext("2d");
......