已经在canvas画布上画出了类似九宫格的形状:
现在想这九个小方块按照设定的轨迹移动最后定格。轨迹没有写 我想知道是怎么样的步骤
贴出代码:function getCursorPosition(e){
var x;
var y;
if (e.pageX != undefined && e.pageY != undefined) {
x = e.pageX;
y = e.pageY;
}else{
//the position relativeto the docment
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
//coordiantes to the canvas
var c = document.getElementById("logo");
x -= c.offsetLeft;
y -= c.offsetTop;
//if the mouse position
if(x>=368 && x<=400 && y>=10 && y<=50){
c_context = c.getContext("2d");
//there have a block box to discovery the text "H"
c_context.fillStyle = "#323232";
c_context.beginPath();
c_context.moveTo(368,10);
c_context.lineTo(400,10);
c_context.lineTo(400,50);
c_context.lineTo(368,50);
c_context.fill();
//there have nine orange box instead if the text "H"
c_context.fillStyle = "#f99405";
for(i = 370 ;i<411; i+=14){
for(j=10; j<51;j+=14){
c_context.beginPath();
c_context.moveTo(i,j);
c_context.lineTo(i+13,j);
c_context.lineTo(i+13,j+13);
c_context.lineTo(i,j+13);
c_context.fill();
//the boxes move to anywhere they want
//这里是想加的代码 但不知道怎么写。(每画出一个小方块就按照预定的轨迹移动) }
}
}
}这个方法的是根据鼠标在canvas上的坐标来判断是否执行小方块的运动轨迹
现在想这九个小方块按照设定的轨迹移动最后定格。轨迹没有写 我想知道是怎么样的步骤
贴出代码:function getCursorPosition(e){
var x;
var y;
if (e.pageX != undefined && e.pageY != undefined) {
x = e.pageX;
y = e.pageY;
}else{
//the position relativeto the docment
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
//coordiantes to the canvas
var c = document.getElementById("logo");
x -= c.offsetLeft;
y -= c.offsetTop;
//if the mouse position
if(x>=368 && x<=400 && y>=10 && y<=50){
c_context = c.getContext("2d");
//there have a block box to discovery the text "H"
c_context.fillStyle = "#323232";
c_context.beginPath();
c_context.moveTo(368,10);
c_context.lineTo(400,10);
c_context.lineTo(400,50);
c_context.lineTo(368,50);
c_context.fill();
//there have nine orange box instead if the text "H"
c_context.fillStyle = "#f99405";
for(i = 370 ;i<411; i+=14){
for(j=10; j<51;j+=14){
c_context.beginPath();
c_context.moveTo(i,j);
c_context.lineTo(i+13,j);
c_context.lineTo(i+13,j+13);
c_context.lineTo(i,j+13);
c_context.fill();
//the boxes move to anywhere they want
//这里是想加的代码 但不知道怎么写。(每画出一个小方块就按照预定的轨迹移动) }
}
}
}这个方法的是根据鼠标在canvas上的坐标来判断是否执行小方块的运动轨迹
解决方案 »
- iframe.name和iframe的window.name是不是一个东西?
- js如何判断ActiveX插件是否已经安装?
- 如何让Iframe所在的页面自适应iframe的高度问题。
- firefox下如何用canvas画带箭头直线
- javascript网页能不能 读取文本文件上的内容,把它们显示到 网页上去呢?
- 双击textarea中有的文本时,怎样取得被选中的文本?
- 请问<script>中src的内容是否为动态加载?
- 请问有没有把表单里的文字转为URLENcode编码的函数?
- 请问有没有这样的写法 document.all.disable = true 或 false 使整个页面的元数失效或有效?
- 求一个正则表达式100%匹配
- 淘宝商城导航栏如何做呢?(在线等哦,js高手,或者比我高的高手都帮帮我这个后起之秀吧!)
- 求教一个jquery的问题
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Playing with Canvas</title>
<script type="text/javascript" >
window.onload = function(){
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
context.fillStyle = "#f99405";
for( i=0;i<100;i++){
context.restore();
context.beginPath();
context.moveTo(i,i);
context.lineTo(i+10,i);
context.lineTo(i+10,i+10);
context.lineTo(i,i+10);
context.fill();
context.save();
context.translate(1,0);
context.clearRect(i,i,i+10,i+10);
}
}</script>
<body>
<canvas id="canvas"></canvas>
</body>
</html>那位帮忙改一下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Playing with Canvas</title>
<script type="text/javascript" >
window.onload = function(){
setInterval(move1,100);
var cxt = document.getElementById("canvas").getContext('2d');
cxt.fillStyle="#f99405";
cxt.translate(50,50);
setInterval(move2,100);
}
function move1(){
var cxt = document.getElementById("canvas").getContext('2d');
cxt.fillStyle="#f99405";
cxt.save();
//cxt.fillRect (0,0, 20,20);
cxt.clearRect(0,0,20,20);
cxt.translate(1,0);
cxt.fillRect (0,0, 20,20);
}
function move2(){
var cxt = document.getElementById("canvas").getContext('2d');
cxt.fillStyle="#f99405";
cxt.save();
cxt.clearRect(0,0,20,20);
cxt.translate(1,0);
cxt.fillRect (0,0, 20,20);
//cxt.fillRect (0,0, 20,20);
}
</script>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
请问 这段代码是逻辑有错吗? 为什么只有一个小方块在运行.????
你的move1和move2条用的都是同一个画布 你说能画出两个图形吗?