javaScript canvas实现_javascript技巧_脚本之家

实例如下所示:

以上这篇javaScript
canvas实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

             shape rectangle circle line        var shap = 99; //0 is circle; 1 is rectangle var orignalX, orignalY;//the coordinate of mouse down var lastX, lastY;//the coordinate of last mouse position var isMouseDown = false; // flag of mouse pressing down var myCanvas = document.getElementById; var context = myCanvas.getContext; var width = myCanvas.width, height = myCanvas.height; var data;//storing last canvas' content context.strokeStyle = "black"; context.strokeWidth=1; context.lineWidth = 1; document.getElementById.onchange = function(){ context.strokeStyle = this.value }; function doEraser(){ context.strokeStyle = "white"; shap = 2; } function sizeChange(){ context.lineWidth = parseInt(document.getElementById; } function shapeChange(){ context.strokeStyle = "black"; var myselect = document.getElementById; var index=myselect.selectedIndex ; var myvalue = myselect.options[index].value; var mytext=myselect.options[index].text; shap = parseInt; } function myCanvasMouseDown { //event.preventDefault(); if { orignalX = event.offsetX; orignalY = event.offsetY; context.moveTo; data = context.getImageData; isMouseDown = true; } } function myCanvasMouseMove { if { //event.preventDefault{ case 0: context.clearRect; context.putImageData; lastX = event.offsetX; lastY = event.offsetY; context.beginPath(); context.arc(orignalX+/2,orignalY+/2,Math.abs/2,0,Math.PI * 2,true); context.stroke(); context.closePath(); break; case 1: context.clearRect; context.putImageData; lastX = event.offsetX; lastY = event.offsetY; context.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY); break; case 2: lastX = event.offsetX; lastY = event.offsetY; context.lineTo; //根据鼠标路径绘画 context.stroke(); //立即渲染 break; } } } function myCanvasMouseUp { if { //event.preventDefault(); context.clearRect; context.putImageData; lastX = event.offsetX; lastY = event.offsetY; switch{ case 0: context.beginPath(); context.arc(orignalX+/2,orignalY+/2,Math.abs/2,0,Math.PI * 2,true); context.stroke(); context.closePath(); break; case 1: context.beginPath(); context.strokeRect(orignalX, orignalY, lastX-orignalX, lastY-orignalY); context.closePath(); break; case 2: context.lineTo; //根据鼠标路径绘画 context.stroke(); //立即渲染 break; } isMouseDown = false; lastX = null; lastY = null; orignalX = null; orignalY = null; data = context.getImageData; context.beginPath(); context.clearRect; context.putImageData; context.closePath(); } } myCanvas.addEventListener("mousedown", myCanvasMouseDown, false); myCanvas.addEventListener("mousemove", myCanvasMouseMove, false); myCanvas.addEventListener("mouseup", myCanvasMouseUp, false);  

相关文章

发表评论