javaScript canvas实现(画笔大小 颜色 橡皮…
2018-06-24 00:54:50来源:未知 阅读 ()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div>
<div>
<!--<input type="button" id="open" value="open"></input>
<input type="button" id= "save" value = "save"></input> <input type="button" id= "color" value="color"></input> -->
<input type="button" value="size"></input>
<input type="text" id="size" onchange="sizeChange()"></input>
<input type="button" id="clear" value="clear"></input>
<input type="button" id="eraser" value="eraser" onclick="doEraser()"></input>
<select id = "shape" onchange="shapeChange()">
<option value = "99">shape</option>
<option value = "1">rectangle</option>
<option value = "0">circle</option>
<option value = "2">line</option>
</select>
<input id="color" type="color"/>
</div>
<canvas id="myCanvas" style=" border:1px solid;" width="800" height="500"></canvas>
</div>
</body>
<script language="JavaScript">
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("myCanvas");
var context = myCanvas.getContext('2d');
var width = myCanvas.width, height = myCanvas.height;
var data;//storing last canvas' content
context.strokeStyle = "black";
context.strokeWidth=1;
context.lineWidth = 1;
document.getElementById('color').onchange = function(){
context.strokeStyle = this.value
};
function doEraser(){
context.strokeStyle = "white";
shap = 2;
}
function sizeChange(){
context.lineWidth = parseInt(document.getElementById('size').value);
}
function shapeChange(){
context.strokeStyle = "black";
var myselect = document.getElementById("shape");
var index=myselect.selectedIndex ;
var myvalue = myselect.options[index].value;
var mytext=myselect.options[index].text;
shap = parseInt(myvalue);
}
function myCanvasMouseDown(event) {
//event.preventDefault();
if(event.button == 0) {
orignalX = event.offsetX;
orignalY = event.offsetY;
context.moveTo(orignalX,orignalY);
data = context.getImageData(0, 0, width, height);
isMouseDown = true;
}
}
function myCanvasMouseMove(event) {
if (isMouseDown){
//event.preventDefault();
switch(shap){
case 0:
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
context.beginPath();
context.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/2,0,Math.PI * 2,true);
context.stroke();
context.closePath();
break;
case 1:
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
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(lastX, lastY); //根据鼠标路径绘画
context.stroke(); //立即渲染
break;
}
}
}
function myCanvasMouseUp(event) {
if (isMouseDown){
//event.preventDefault();
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
lastX = event.offsetX;
lastY = event.offsetY;
switch(shap){
case 0:
context.beginPath();
context.arc(orignalX+(lastX-orignalX)/2,orignalY+(lastY-orignalY)/2,Math.abs(lastX-orignalX)/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(lastX, lastY); //根据鼠标路径绘画
context.stroke(); //立即渲染
break;
}
isMouseDown = false;
lastX = null;
lastY = null;
orignalX = null;
orignalY = null;
data = context.getImageData(0, 0, width, height);
context.beginPath();
context.clearRect(0,0,width,height);
context.putImageData(data,0,0);
context.closePath();
}
}
myCanvas.addEventListener("mousedown", myCanvasMouseDown, false);
myCanvas.addEventListener("mousemove", myCanvasMouseMove, false);
myCanvas.addEventListener("mouseup", myCanvasMouseUp, false);
</script>
</html>
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- javascript面向对象入门基础详细介绍 2020-03-29
- js防止表单重复提交实现代码 2020-03-29
- 基于JQuery的多标签实现代码 2020-03-29
- js实现翻页后保持checkbox选中状态的实现方法 2020-03-25
- JavaScript函数表达式详解及实例 2020-03-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash