【发布时间】:2016-05-26 21:34:10
【问题描述】:
当我尝试通过按“s”然后按“l”来“加载”“保存的绘图”时,它会导致浏览器停止响应。我认为这是因为所有的 while 循环(第 47 行及更高),但即使我添加的 setTimeout 内容也无济于事。帮助将不胜感激。 此脚本嵌入我的网站 (https://veryblankwhitepaper.weebly.com/with-canvas.html):
var canvas = document.getElementById("draw");
var context = canvas.getContext("2d");
var color = "#0000ff";
var xs = [];
var ys = [];
var colors = [];
var sizes = [];
var loadDrawing = function(){
for(var p = 0; p < xs.length; p++){
setTimeout(function(){
context.beginPath();
context.moveTo(xs[p], ys[p]);
context.fillStyle = colors[p];
context.arc(xs[p] - (sizes[p] / 2), ys[p] - (sizes[p] / 2), sizes[p], 0, Math.PI * 2);
context.fill();
context.closePath();
},10);
}
alert("done!");
}
var isMouseDown = false; var thickness = 4;
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
window.onkeyup = function(e){
e = e || e.which;
var key = e.which || e.keycode;
if(key === 65){
color = prompt("Change the color");
}
if(key === 76){
if(confirm("Do you want to load a saved picture?") === true){
var load = prompt("What file do you want to load","insert saved file name here");
context.clearRect(0,0,100000,100000);
alert("please wait. this may take some time depending on how big the drawing is..");
xs = [];
ys = [];
colors = [];
sizes = [];
var int = 0;
var str = "";
while(localStorage[load + "xs"][int] !== ""){
while(localStorage[load + "xs"][int] !== ","){
setTimeout(function(){
str = str + localStorage[load + "xs"][int];
int = int + 1;
},10);
}
int = int + 1;
xs.push(str);
str = "";
}
int = 0;
while(localStorage[load + "ys"][int] !== ""){
while(localStorage[load + "ys"][int] !== ","){
setTimeout(function(){
str = str + localStorage[load + "ys"][int];
int = int + 1;
},10);
}
int = int + 1;
ys.push(str);
str = "";
}
int = 0;
while(localStorage[load + "colors"][int] !== ""){
while(localStorage[load + "colors"][int] !== ","){
setTimeout(function(){
str = str + localStorage[load + "colors"][int];
int = int + 1;
},10);
}
int = int + 1;
colors.push(str);
str = "";
}
int = 0;
while(localStorage[load + "sizes"][int] !== ""){
while(localStorage[load + "sizes"][int] !== ","){
setTimeout(function(){
str = str + localStorage[load + "sizes"][int];
int = int + 1;
},10);
}
int = int + 1;
sizes.push(str);
str = "";
}
loadDrawing();
}
}
if(key === 83){
if(confirm("Do you want to save the drawing?") === true){
var answer = prompt("Enter a name for the drawing.");
localStorage[answer + "xs"] = xs;
localStorage[answer + "ys"] = ys;
localStorage[answer + "colors"] = colors;
localStorage[answer + "sizes"] = sizes;
}
}
if(key === 66){
thickness = prompt("change thinkness (in pixels)");
}
if(key === 67){
if(confirm("clear the canvas?") === true){
context.clearRect(0,0,10000,10000);
}
}
}
window.onmousedown = function() {
isMouseDown = true;
}
window.onmouseup = function () {
isMouseDown = false;
}
window.onmousemove = function(e) {
if(isMouseDown === true){
xs.push(getMousePos(canvas, e).x);
ys.push(getMousePos(canvas, e).y);
colors.push(color);
sizes.push(thickness);
context.beginPath();
context.moveTo(getMousePos(canvas, e).x, getMousePos(canvas, e).y);
context.fillStyle = color;
context.arc(getMousePos(canvas, e).x - (thickness / 2), getMousePos(canvas, e).y - (thickness / 2), thickness, 0, Math.PI * 2);
context.fill();
context.closePath();
}
}
【问题讨论】:
-
即使您的 question 可能会在您的开头段落/标题中暗示,但问一个仍然很好。你有什么问题?
-
我有几个问题:有人可以帮我解决这个问题吗?为什么会这样?
-
那些
while循环的结构完全被破坏了,将永远循环,直到你耗尽计时器资源。请在您的问题中添加您要通过这些while循环完成的工作。无论是什么,都必须以完全不同的方式完成。 -
while 循环应该从 localStorage 字符串中获取每个值并将其转回数组。因此它获取逗号之间的项目并将其添加到列表中,直到字符串的项目“int”为空。
-
在代码的任何阶段,您希望 str 的值在 setTimeout() 调用内部更新并在它们外部引用?
标签: javascript function while-loop settimeout