实例如下所示:
<!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>
以上这篇javaScript canvas实现(画笔大小 颜色 橡皮的实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
canvas,画笔大小
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“javaScript canvas实现(画笔大小 颜色 橡皮的实例)”评论...
更新动态
2024年11月13日
2024年11月13日
- 《忆蚀》Subliminal:揭秘后室之谜,路知行献声Weplay文化展
- 那英《征服NEWXRCD台湾版》日本压制[WAV+CUE]
- 群星《金曲百分百上》3CD(香港版)[WAV+CUE]
- 刘欢《雨中的树(新歌加精选)2CD》德国HD24K金碟[WAV+CUE]
- 郑源 《世间情歌》6N纯银SQCD[WAV+CUE][1G]
- 群星《粤潮2HQII》头版限量编号[低速原抓WAV+CUE][991M]
- 群星《2023好听新歌21》十倍音质 U盘音乐[WAV分轨][1G]
- 《热血传奇》双11感恩回馈 超值狂欢30天
- 原神5.2版本活动汇总 5.2版本活动有哪些
- 张敬轩.2010-NO.ELEVEN【环球】【WAV+CUE】
- 黄丽玲.2006-失恋无罪【艾回】【WAV+CUE】
- 阿达娃.2024-Laluna【W8VES】【FLAC分轨】
- 宝可梦大集结段位等级划分表大全 大集结段位一览
- 龙腾世纪影障守护者工坊与装备如何升级 工坊与装备升级说明
- 龙腾世纪影障守护者全成就攻略分享 龙腾世纪4全成就列表一览