javascript - firefox4还不支持canvas吗?

标签 javascript html canvas

我尝试了以下代码,但 Firefox4 没有显示任何内容..

<script type="text/javascript">

var canvas = document.getElementById('canvas1');

var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>

 <canvas id="canvas1" width="200px" height="200px">Your browser does not   support canvas </canvas>

此代码位于 body 标记内。

最佳答案

您必须了解浏览器执行文件的顺序。 以下是浏览器读取此代码的方式:

<script type="text/javascript">

var canvas = document.getElementById('canvas1');

var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>

 <canvas id="canvas1" width="200px" height="200px">Your browser does not   support canvas </canvas>
  1. 正文标记开始
  2. 脚本标记开始
  3. 查找 ID 为 canvas1 的元素
  4. Canvas1不存在,将canvas保存为null(我当作什么都没有)
  5. Context = nothing.getContext('2d');
  6. 警报(无)
  7. 结束脚本
  8. 开始 Canvas ,因为支持不显示里面的内容
  9. 正文结束标记
<小时/>

因此,要使其可见,您只需像这样编辑代码:-

<!DOCTYPE HTML>
<html>
<head>
 <title>Sample by RHNVRM(aka rohan verma)</title>
</head>
<body>
<!--Canvas-->
 <canvas id="canvas1" width="200px" height="200px">Your browser does not   support canvas </canvas>
<!--Begin Script-->
<script>

var canvas = document.getElementById('canvas1');

var context = canvas.getContext('2d');
context.strokeStyle = '#990000';
context.strokeRect(20,30,100,50);
alert(context);
</script>
</body>
</html>
<小时/>

注意: 使用 Javascript 时无需在 HTML5 中提及。

关于javascript - firefox4还不支持canvas吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6314691/

相关文章:

javascript - 如何向 Google V8 添加新类?

javascript - 提交表单时,我不想在出现弹出窗口时加载页面

java - 无法使用 XPath 识别页面上的所有文本元素

javascript - 如何使用 jQuery 在电子邮件验证上的按键上添加另一个语句

delphi - 绘制的矩形总是在创建下一个时被删除

javascript - 使用ng-repeat、ng-form、ng-submit后如何提交

javascript - 用 JavaScript 填充表格

html - 如何划线边框内的所有链接并加粗标记段落的某些文本

math - 如何缩放贝塞尔曲线(使用 HTML5 Canvas )?

html - 以编程方式在html Canvas 上绘制svg文件