我想要使用JavaScript來繪製一個簡單的應用程序,當我按下屏幕時(在觸摸屏上)繪製一個矩形。我不知道它爲什麼不起作用。下面是代碼:當屏幕被按下時,JavaScript繪製一個矩形
HTML
<canvas id="drawingboard" width="500" height="500" style="border:10px solid #000000;">
You have a horrible browser that will not support an HTML canvas, so this won't work.
</canvas>
的JavaScript
var c = document.getElementById("drawingboard");
var ctx = c.getContext("2d");
var mouseDown = 0;
document.body.ontouchstart = function() {
mouseDown+=1;
}
document.body.ontouchend = function() {
mouseDown-=1;
}
if (mouseDown == 1) {
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);}
您需要在事件處理程序中繪製。 –