我该如何将这些代码放在一起?

时间:2014-03-14 21:13:56

标签: javascript canvas

我有一个Javascript程序在三个不同的地方工作:here, here,here. 但我不知道如何将它们放在一个文件中。这就是我现在所处的位置。

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<style>
body {
background-color:#E4E0FF;
}
#canvas {
border:10px solid red;
background-color:white;
}
#canvas2 {
border:10px solid red;
background-color:white;
}
#canvas3 {
border:10px solid red;
background-color:white;
}
</style>
 </head>
 <body>
<canvas id="canvas" width="400" height="300">
</canvas>
<canvas id="canvas2" width="400" height="300">
</canvas>
<canvas id="canvas3" width="400" height="300">
</canvas>
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>

<script type="text/javascript">
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();

var toggle=0;
var x=150;
var y=100;
var w=100;
var h=100;
var r=60;
var wasInside=false;

ctx.fillStyle = "#000000";
ctx.fillRect(x, y, w, h);

function changeColor() {
if (toggle == 0) {
ctx.fillStyle = "#04B45F";
toggle = 1;
} else if (toggle ==1){
ctx.fillStyle = "#04B45F";
toggle = 2;
}else if (toggle == 2){
ctx.fillStyle = "#0000FF";
toggle = 3;
}else if (toggle == 3){
ctx.fillStyle == "#190707";
toggle = 4;
}else if (toggle == 4){
ctx.fillStyle = "#210B61";
toggle = 5;
}else if (toggle == 5){
ctx.fillStyle = "#FA58AC";
toggle = 6;
}else if (toggle ==6){
ctx.fillStyle = "#FFFF00";
toggle = 7;
}else{
ctx.fillStyle = "#F5A9D0";
toggle = 0;
}

ctx.fillRect(x, y, w, h);
}

 function changeRadius() {
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0,0, 400, 300);
    r = Math.floor((Math.random() * 80) + 20);
    ctx.beginPath();
    ctx.arc(200, 150, r, 0, 2 * Math.PI);
    ctx.stroke();
  }

  function changeWidth() {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0,0, 400, 300);    
width = Math.floor((Math.random()*50)+1);
ctx.lineWidth=width;
ctx.stroke();

}

 function handleMouseMove(e) {
 var mx = parseInt(e.clientX - offsetX);
 var my = parseInt(e.clientY - offsetY);

 var isInsideNow = (mx > x && mx < x + w && my > y && my <= y + h);

if (isInsideNow && !wasInside) {
changeColor();
wasInside = true;
} else if (!isInsideNow && wasInside) {
wasInside = false;
}

}

$("#canvas").mousemove(function (e) {
handleMouseMove(e);
});
$("#canvas2").mousemove(function (e) {
handleMouseMove(e);
});
$("#canvas3").mousemove(function (e) {
handleMouseMove(e);
});

</script>
</body>
</html>

那么,我怎么告诉程序在第二个画布上做圆形的东西并在第三个画布中排成一行? 我还没有调用changeWidth和changeRadius函数,因为它只是在第一个画布上弄乱了。 我只需要在代码的这一部分中使用某些东西来调用不同画布中的不同函数

 if (isInsideNow && !wasInside) {
     changeColor();
     wasInside = true;
 } else if (!isInsideNow && wasInside) {
     wasInside = false;
 }

1 个答案:

答案 0 :(得分:0)

我不是画布专家,只是为了给你一个想法,请查看FIDDLE

var c = document.getElementById("canvas");
var ctx = c.getContext("2d");

检查这两行,上下文ctx将在画布中用id=canvas绘制。您可以创建其他上下文变量以在其他画布中绘制或重复使用它。

已更新FIDDLE

更新更新:FIDDLE