实例如下所示:

<!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实现(画笔大小 颜色 橡皮的实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?