HTML5畫板可以通過使用canvas元素和JavaScript來創建一個優雅而富有創意的畫布。在這種情況下,我們可以使用HTML5畫板鉛筆,它可以幫助我們在Users界面上快速而可靠地繪制對象。
var canvas,context; var painting=false; var lastX,lastY; function init(){ canvas=document.getElementById('drawing-area'); context=canvas.getContext('2d'); context.lineWidth=5; context.lineJoin='round'; context.lineCap='round'; context.strokeStyle='blue'; addEventHandler(canvas,'mousedown',function(e){ var mouseX=e.pageX-canvas.offsetLeft; var mouseY=e.pageY-canvas.offsetTop; painting=true; addClick(mouseX,mouseY,false); draw(); }); addEventHandler(canvas,'mousemove',function(e){ if(painting){ addClick(e.pageX-canvas.offsetLeft,e.pageY-canvas.offsetTop,true); draw(); } }); addEventHandler(canvas,'mouseup',function(e){ painting=false; }); addEventHandler(canvas,'mouseleave',function(e){ painting=false; }); } function addEventHandler(obj,eventName,handler){ if(obj.addEventListener){ obj.addEventListener(eventName,handler,false); } else if(obj.attachEvent){ obj.attachEvent('on'+eventName,handler); } else { obj['on'+eventName]=handler; } } function addClick(x,y,drawing){ clickX.push(x); clickY.push(y); clickDrag.push(drawing); } function draw(){ context.clearRect(0,0,canvas.width,canvas.height); // Clears the canvas context.beginPath(); context.moveTo(clickX[0], clickY[0]); for(var i=1; i在這段HTML5畫板鉛筆代碼中,我們定義了一系列事件處理程序,以便在用戶輸入新繪畫之后呈現出更完整的畫布。