简体   繁体   English

HTML5 Canvas Javascript使我的网页链接无响应

[英]Html5 Canvas Javascript making my webpage links unresponsive

I have an html5 canvas animation. 我有一个html5画布动画。 When it runs it makes my page unresponsive or rather my links to navigate the page become unresponsive. 当它运行时,会使我的页面无响应,或者导航页面的链接无响应。 No code is directly doing this. 没有代码直接这样做。 I think it is because the javascript is slowing down the browser to much. 我认为这是因为javascript会大大降低浏览器的速度。 Anyways heres a jsfiddle to the code 无论如何,这是代码的jsfiddle

http://jsfiddle.net/#&togetherjs=psm0e5RNeI http://jsfiddle.net/#&togetherjs=psm0e5RNeI

When I remove the include main javascript file (the one in fiddle) the links work fine. 当我删除包含主javascript文件(小提琴一个)时,链接可以正常工作。 If I comment out the setInterval timer in the code the web page remains unresponsive. 如果我在代码中注释掉setInterval计时器,则网页将保持无响应。 If I comment out the calls to the cloud and light function under the draw function which is drawing elements based off of the 2 other libraries the web page still remains unresponsive. 如果我注释掉对draw函数下的cloud and light函数的调用,该函数是根据其他2个库绘制元素的,则该网页仍然没有响应。

Could someone help me figure out why this is happening? 有人可以帮我弄清楚为什么会这样吗?

Thank you! 谢谢!

I would also like to add there are no errors in the web console as well. 我还想补充一下,Web控制台中也没有错误。

Here I also decided to include the javascript: 在这里,我还决定添加javascript:

var canvas = document.getElementById('canvas'),
context=canvas.getContext('2d');


var fullC = Math.PI * 2; // Ending circle angle


var cW = canvas.width; // canvas width

var cH = canvas.height; // canvas height


var sizeInc = 0;


var globCount = 0; // how many times start is called


// cloud vars //

var radius = 90; // radious for clouds


var clX = radius / 2;

var clY = radius / 2;


var centreX = context.canvas.width / 2;

var centreY = context.canvas.height / 2;


var colour = {r:255, g:255, b:254};


var alpha = 0.2;


var circles = 20; // number of circles in the cloud 


// rain vars //

var rain = [];

var rainC = 55;


/// Rain objects ///

rain[0] = {

radius:2.5,
col:'red',
speed: getRandomInt(5,20),
ry:0,
rx:getRandomInt(0,canvas.width + 1000)


};


for(var i = 1; i <= rainC; i++){

rain[i] = {

    radius:2.5,
    col:'red',
    speed: getRandomInt(5,20),
    ry:0,
    rx:getRandomInt(0,canvas.width + 1000)


};

}


/// Lightning ///


var lt = new lightning({glow: false});

var drawL = false;


/// Starts the Animation ///

function start(){

globCount ++;

clearIt();
setBack();
draw();

}


/// Draw calls all the components of the animation ///

function draw(){

 clouds();
 rainIt();
 light();

}



/// Lightning Function ///

function light(){

var modResult = globCount % 50;

var modResult1 = globCount % 10;

if(modResult === 0){

    drawL = true;
}
if(modResult1 === 0){

    lt.hide();

}

if(drawL === true){

    lt.hide();
    lt.show(getRandomInt(100,canvas.width - 25),800, getRandomInt(100,canvas.width), canvas.height);
    drawL = false;
}


}


/// Draws the clouds ///

function clouds(){

/// Draws a concentrated cluster and a sparse cluster ///
for(var i = 0; i <= 14; i++){

    $cloudgen.drawCloud(context, clX + (i * radius) - 10, clY, radius + 20, colour, alpha, circles);

    $cloudgen.drawCloud(context, clX + (i * radius) - 10, clY, radius, colour, 0.15, circles);

}

}



/// Rain function ///

function rainIt(){

/// Draws all the rain ///
rain.forEach(function(rain){

    context.beginPath();
    context.fillStyle = 'rgba(102,217,239,.5)';
    context.arc(rain.rx,rain.ry,rain.radius,0,fullC);
    context.closePath();
    context.fill();

    /// Resets rains x y points ///
    if(rain.ry > canvas.height || rain.ry < 0){

        rain.ry = 0;
        rain.rx = getRandomInt(0,canvas.width);

    }

    if(rain.rx > canvas.width || rain.rx < 0){

        rain.ry = 0;
        rain.rx = getRandomInt(0,canvas.width);

    }

    rain.rx = rain.rx + getRandomInt(1,12);

    rain.ry = rain.ry + rain.speed;

});


}


/// Sets the background ///

function setBack(){

context.fillStyle = 'rgba(61,131,227,0.5)';
context.fillRect(0,0,cW,cH);


}


/// Clears Canvas ///

function clearIt(){

context.clearRect(0,0,canvas.width,canvas.height);


}


/// Random number maker ///

function getRandomInt (min, max) {

return Math.floor(Math.random() * (max - min + 1)) + min;

}


setInterval(start,100);

You use some lightening code, it creates canvas which is overlay all other elements. 您使用一些简化代码,它创建了覆盖所有其他元素的画布。 So it blocks other elements from mouse input. 因此,它会阻止鼠标输入中的其他元素。 Here what I see in chrome debugging tools: 这是我在chrome调试工具中看到的内容:

<canvas width="1024px" height="888px" id="lightning_canvas" style="position: absolute; top: 0px; left: 0px;"></canvas>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM