javascript动画不适用于chrome和IE
javascript animation not working on chrome and IE
这是一个类似矩阵的动画,在Firefox上测试时效果非常好,但在Chrome和IE上只显示按钮。它与更改颜色的函数有关,但实际上,我在代码中包含的任何额外函数都会使整个事情无法工作。
var c = document.getElementById("c");
var ctx = c.getContext("2d");
var color = "blue";
// setting canvas dimension
c.height = 500;
c.width = 500;
//the numbers that will be printed out
var numbers = "0123456789";
//create an array of single numbers
numbers = numbers.split("");
var font_size = 12;
var columns = c.width/font_size; //number of columns for the rain
//create an array of drops - one per column
var matrix = [];
for(var x = 0; x < columns; x++)
matrix[x] = c.height/font_size+1;
//functions to change the colour of the matrix
function setBlue() color = "blue";
function setGreen() color = "green";
function setRed() color = "red";
//drawing the matrix
function draw() {
//black background with transparency so that the drops leave trail
ctx.fillStyle = "rgba(0, 0, 0, 0.05)";
ctx.fillRect(0, 0, c.width, c.height);
if (color == "blue"){
ctx.fillStyle = "#00ffd2";
}else if (color == "green"){
ctx.fillStyle = "#0f0";
}else if (color == "red"){
ctx.fillStyle = "#ff0008";
}
ctx.font = font_size + "px papyrus";
//loop the drawing
for(var i = 0; i < matrix.length; i++)
{
//print random character
var text = numbers[Math.floor(Math.random()*numbers.length)];
ctx.fillText(text, i*font_size, matrix[i]*font_size);
//add randomness and send the character to the top of the screen
if(matrix[i]*font_size > c.height && Math.random() > 0.95)
matrix[i] = 0;
matrix[i]++;
}
}
setInterval(draw, 30);
哎呀!您的函数定义格式不正确。。。
试试这个:
//functions to change the colour of the matrix
function setBlue(){ color = "blue";}
function setGreen(){ color = "green";}
function setRed(){ color = "red";}
祝你的项目好运!
相关文章:
- IE/Chrome中未定义的函数,但Firefox中没有
- 滚动在Chrome中有效,但在Firefox或IE中无效
- CKeditor预览插件.js,为Chrome和IE进行编辑.因为它拒绝CSS样式
- 如何编写在Chrome和IE中正常工作的JavaScript
- Javascript/AAJAX在Opera中不起作用,在FF/IE/Chrome中完美工作
- JavaScript可以在IE+Chrome中使用,但不能在FF中使用
- 在 Java 中使用 IE/Chrome HTML 渲染器或使用 JavaScript 存储文件
- Javascript - 附加到文本区域值 - IE Chrome 和 Firefox
- Src 更改在 Firefox 中工作,而不是在 IE/Chrome 中
- HTML5游戏-工作在IE, Chrome.在FireFox中失败…直到我导航到一个.mp3文件…然后它才工作
- JQuery脚本,适用于Firefox,而不是在IE, Chrome
- jquery不工作在IE, Chrome,工作在FF
- 创建CSV并打开JS不工作在IE, Chrome
- OpenLayers 3旋转示例在Win32触摸设备上的IE/Chrome嵌入式WebBrowser控件中表现不同
- preventDefault()对一个多选择,不同的行为在Firefox - IE - Chrome
- Js工作在IE, Chrome,但不是Firefox
- Ie /chrome检查器导致重画
- 展开嵌套无序列表和水平显示在IE/Chrome/FF
- Firefox与IE/Chrome中的事件处理程序
- Firefox将文本叠加在错误的位置,而不是在Firefox中工作(在IE, Chrome和safari中很好)