使用鼠标在HTML5画布上绘制

使用鼠标在HTML5画布上绘制,html,canvas,gesture,gesture-recognition,Html,Canvas,Gesture,Gesture Recognition,我想使用鼠标在HTML画布上绘制(例如,绘制签名、绘制名称等) 我该如何实现这个呢?谷歌搜索了这个(“html5画布绘制程序”)。看起来像是你需要的 如果您在执行此操作时遇到问题,请告诉我。它使用processing.js并具有更改颜色和使绘图点越来越大和越来越小的功能 <html> <head> <!--script librarires--> <script type="text/javascript" src="processing.js">

我想使用鼠标在HTML画布上绘制(例如,绘制签名、绘制名称等)

我该如何实现这个呢?

谷歌搜索了这个(“html5画布绘制程序”)。看起来像是你需要的


如果您在执行此操作时遇到问题,请告诉我。它使用processing.js并具有更改颜色和使绘图点越来越大和越来越小的功能

<html>
<head>
<!--script librarires-->
<script type="text/javascript" src="processing.js"></script>
<script type="text/javascript" src="init.js"></script>

<!--styles -->
<style type="text/css" src="stylesheet.css">
</style> 
</head>
<body>
<!--toolbox -->
<div id="draggable toolbox"></div>
<script type="application/processing">
// new script
int prevx, prevy;
int newx, newy;
boolean cliked;
color c1 = #000000;
int largeur=2;
int ps = 20;
int px = 50;
int py = 50;

void setup() {
size(500,500);
frameRate(25);
background(50);

 prevx = mouseX;
 prevy = mouseY;
 cliked = false;
 }

void draw() {
 //couleur
 noStroke(0);
 fill(#FFFFFF);//blanc
rect(px, py, ps, ps);
 fill(#000000);
 rect(px, py+(ps), ps, ps);
 fill(#FF0000);
 rect(px, py+(ps*2), ps, ps);
 fill(#00FF00);
 rect(px, py+(ps*3), ps, ps);
 fill(#FFFF00);
 rect(px, py+(ps*4), ps, ps);
 fill(#0000FF);
 rect(px, py+(ps*5), ps, ps);
 //largeur
 fill(#FFFFFF);
 rect(px, py+(ps*7), ps, ps);
  fill(#FFFFFF);
 rect(px, py+(ps*8), ps, ps);
 stroke(#000000);
 line(px+2, py+(ps*7)+(ps/2), px+(ps-2), py+(ps*7)+(ps/2));
 line(px+(ps/2), py+(ps*7)+1, px+(ps/2), py+(ps*8)-1);
 line(px+2, py+(ps*8)+(ps/2), px+(ps-2), py+(ps*8)+(ps/2));

 if(cliked==false){
  prevx = mouseX;
 prevy = mouseY;  
 }
 if(mousePressed) { 
  cliked = true;
 newx = mouseX;
  newy = mouseY;
  strokeWeight(largeur);
  stroke(c1);
  line(prevx, prevy, newx, newy);
  prevx = newx;
 prevy = newy;
 }else{
  cliked= false;
  }
}
void mouseClicked() {
 if (mouseX>=px && mouseX<=(px+ps)){
  //couleur
  if (mouseY>=py && mouseY<=py+(ps*6)){ 
   c1 = get(mouseX, mouseY);
  }
   //largeur
  if (mouseY>=py+(ps*7) && mouseY<=py+(ps*8)){ 
   largeur += 2;
  }
 if (mouseY>=py+(ps*8) && mouseY<=py+(ps*9)){ 
   if (largeur>2){
    largeur -= 2;
   }
  }
 }
}
</script><canvas></canvas>
</body>
</html>

//新剧本
int-prevx,prevy;
int newx,newy;
布尔陈词滥调;
颜色c1=#000000;
int largeur=2;
int-ps=20;
int-px=50;
int-py=50;
无效设置(){
尺寸(500500);
帧率(25);
背景(50);
prevx=鼠标;
prevy=老鼠;
陈词滥调=错误;
}
作废提款(){
//库勒
仰泳(0);
填充(#FFFFFF);//布兰科
rect(px,py,ps,ps);
填写(#000000);
rect(px,py+(ps),ps,ps);
填充(#FF0000);
rect(px,py+(ps*2),ps,ps);
填充(#00FF00);
rect(px,py+(ps*3),ps,ps);
填充(#FFFF00);
rect(px,py+(ps*4),ps,ps);
填充(#0000FF);
rect(px,py+(ps*5),ps,ps);
//大户
填充(#FFFFFF);
rect(px,py+(ps*7),ps,ps);
填充(#FFFFFF);
rect(px,py+(ps*8),ps,ps);
冲程(#000000);
线(px+2,py+(ps*7)+(ps/2),px+(ps-2),py+(ps*7)+(ps/2));
线(px+(ps/2),py+(ps*7)+1,px+(ps/2),py+(ps*8)-1);
线(px+2,py+(ps*8)+(ps/2),px+(ps-2),py+(ps*8)+(ps/2));
if(cliked==false){
prevx=鼠标;
prevy=老鼠;
}
如果(鼠标按下){
陈词滥调=正确;
newx=鼠标;
纽伊=老鼠;
冲程重量(较大);
中风(c1);
行(prevx,prevy,newx,newy);
prevx=newx;
prevy=newy;
}否则{
陈词滥调=错误;
}
}
void mouseClicked(){
如果(mouseX>=px&&mouseX=py&&mouseY=py+(ps*7)和&mouseY=py+(ps*8)和&mouseY2){
较大者-=2;
}
}
}
}

检查此项。这将指引您正确的方向

以下是使用画布创建绘图应用程序的最简单方法:

  • mousedown
    mousemove
    mouseup
    事件侦听器附加到画布DOM
  • mousedown
    上,获取鼠标坐标,并使用
    moveTo()
    方法定位图形光标,使用
    beginPath()
    方法开始新的图形路径
  • mousemove
    上,使用
    lineTo()
    连续向路径添加一个新点,并使用
    stroke()
    为最后一段上色
  • mouseup
    上,设置一个标志以禁用图形

  • 从那里,您可以添加各种其他功能,例如让用户能够选择线条厚度、颜色、笔划,甚至图层。

    这是一个工作示例

    
    变量画布,ctx,标志=false,
    prevX=0,
    currX=0,
    prevY=0,
    咖喱=0,
    dot_标志=假;
    var x=“黑色”,
    y=2;
    函数init(){
    canvas=document.getElementById('can');
    ctx=canvas.getContext(“2d”);
    w=画布宽度;
    h=画布高度;
    canvas.addEventListener(“mousemove”,函数(e){
    findxy('move',e)
    },假);
    canvas.addEventListener(“鼠标向下”,函数(e){
    findxy('down',e)
    },假);
    canvas.addEventListener(“mouseup”,函数(e){
    findxy('up',e)
    },假);
    canvas.addEventListener(“mouseout”,函数(e){
    findxy('out',e)
    },假);
    }
    功能颜色(obj){
    开关(对象id){
    案例“绿色”:
    x=“绿色”;
    打破
    案例“蓝色”:
    x=“蓝色”;
    打破
    案例“红色”:
    x=“红色”;
    打破
    案例“黄色”:
    x=“黄色”;
    打破
    案例“橙色”:
    x=“橙色”;
    打破
    案例“黑色”:
    x=“黑色”;
    打破
    “白色”案例:
    x=“白色”;
    打破
    }
    如果(x=“白色”)y=14;
    否则y=2;
    }
    函数绘图(){
    ctx.beginPath();
    ctx.moveTo(prevX,prevY);
    ctx.lineTo(咖喱,咖喱);
    ctx.strokeStyle=x;
    ctx.lineWidth=y;
    ctx.stroke();
    ctx.closePath();
    }
    函数擦除(){
    var m=确认(“想要清算”);
    如果(m){
    ctx.clearRect(0,0,w,h);
    document.getElementById(“canvasimg”).style.display=“无”;
    }
    }
    函数save(){
    document.getElementById(“canvasimg”).style.border=“2px solid”;
    var dataURL=canvas.toDataURL();
    document.getElementById(“canvasimg”).src=dataURL;
    document.getElementById(“canvasimg”).style.display=“inline”;
    }
    函数findxy(res,e){
    如果(res=='down'){
    prevX=currX;
    prevY=咖喱;
    currX=e.clientX-canvas.offsetLeft;
    currY=e.clientY-canvas.offsetTop;
    flag=true;
    dot_flag=true;
    if(dot_标志){
    ctx.beginPath();
    ctx.fillStyle=x;
    ctx.fillRect(currX,currY,2,2);
    ctx.closePath();
    dot_标志=假;
    }
    }
    如果(res==“向上”| res==“向外”){
    flag=false;
    }
    如果(res=='move'){
    国际单项体育联合会(旗){
    prevX=currX;
    prevY=咖喱;
    currX=e.clientX-canvas.offsetLeft;
    currY=e.clientY-canvas.offsetTop;
    draw();
    }
    }
    }
    选择颜色
    橡皮擦
    
    我也在寻找将此方法用于签名的方法,我在上找到了一个示例

    我已将以下代码添加到

    Html:

    Alco支票
    <div id="sketch">
        <canvas id="paint"></canvas>
    </div>
    
     (function() {
        var canvas = document.querySelector('#paint');
        var ctx = canvas.getContext('2d');
    
        var sketch = document.querySelector('#sketch');
        var sketch_style = getComputedStyle(sketch);
        canvas.width = parseInt(sketch_style.getPropertyValue('width'));
        canvas.height = parseInt(sketch_style.getPropertyValue('height'));
    
        var mouse = {x: 0, y: 0};
        var last_mouse = {x: 0, y: 0};
    
        /* Mouse Capturing Work */
        canvas.addEventListener('mousemove', function(e) {
            last_mouse.x = mouse.x;
            last_mouse.y = mouse.y;
    
            mouse.x = e.pageX - this.offsetLeft;
            mouse.y = e.pageY - this.offsetTop;
        }, false);
    
    
        /* Drawing on Paint App */
        ctx.lineWidth = 5;
        ctx.lineJoin = 'round';
        ctx.lineCap = 'round';
        ctx.strokeStyle = 'blue';
    
        canvas.addEventListener('mousedown', function(e) {
            canvas.addEventListener('mousemove', onPaint, false);
        }, false);
    
        canvas.addEventListener('mouseup', function() {
            canvas.removeEventListener('mousemove', onPaint, false);
        }, false);
    
        var onPaint = function() {
            ctx.beginPath();
            ctx.moveTo(last_mouse.x, last_mouse.y);
            ctx.lineTo(mouse.x, mouse.y);
            ctx.closePath();
            ctx.stroke();
        };
    
    }());
    
    <canvas id="canvas" width="490" height="220"></canvas>
    
    context = document.getElementById('canvas').getContext("2d");
    
    $('#canvas').mousedown(function(e){
      var mouseX = e.pageX - this.offsetLeft;
      var mouseY = e.pageY - this.offsetTop;
    
      paint = true;
      addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
      redraw();
    });
    
    $('#canvas').mouseup(function(e){
      paint = false;
    });
    
    $('#canvas').mouseleave(function(e){
      paint = false;
    });
    
    var clickX = new Array();
    var clickY = new Array();
    var clickDrag = new Array();
    var paint;
    
    function addClick(x, y, dragging)
    {
      clickX.push(x);
      clickY.push(y);
      clickDrag.push(dragging);
    }
    
    //Also redraw
    function redraw(){
      context.clearRect(0, 0, context.canvas.width, context.canvas.height); // Clears the canvas
    
      context.strokeStyle = "#df4b26";
      context.lineJoin = "round";
      context.lineWidth = 5;
    
      for(var i=0; i < clickX.length; i++) {        
        context.beginPath();
        if(clickDrag[i] && i){
          context.moveTo(clickX[i-1], clickY[i-1]);
         }else{
           context.moveTo(clickX[i]-1, clickY[i]);
         }
         context.lineTo(clickX[i], clickY[i]);
         context.closePath();
         context.stroke();
      }
    }
    
    <html>
        <script type="text/javascript">
        var canvas, canvasimg, backgroundImage, finalImg;
        var mouseClicked = false;
        var prevX = 0;
        var currX = 0;
        var prevY = 0;
        var currY = 0;
        var fillStyle = "black";
        var globalCompositeOperation = "source-over";
        var lineWidth = 2;
    
        function init() {
          var imageSrc = '/abstract-geometric-pattern_23-2147508597.jpg'
          backgroundImage = new Image();
          backgroundImage.src = imageSrc;
          canvas = document.getElementById('can');
          finalImg = document.getElementById('finalImg');
          canvasimg = document.getElementById('canvasimg');
          canvas.style.backgroundImage = "url('" + imageSrc + "')";
          canvas.addEventListener("mousemove", handleMouseEvent);
          canvas.addEventListener("mousedown", handleMouseEvent);
          canvas.addEventListener("mouseup", handleMouseEvent);
          canvas.addEventListener("mouseout", handleMouseEvent);
        }
    
        function getColor(btn) {
          globalCompositeOperation = 'source-over';
          lineWidth = 2;
          switch (btn.getAttribute('data-color')) {
            case "green":
            fillStyle = "green";
            break;
            case "blue":
            fillStyle = "blue";
            break;
            case "red":
            fillStyle = "red";
            break;
            case "yellow":
            fillStyle = "yellow";
            break;
            case "orange":
            fillStyle = "orange";
            break;
            case "black":
            fillStyle = "black";
            break;
            case "eraser":
            globalCompositeOperation = 'destination-out';
            fillStyle = "rgba(0,0,0,1)";
            lineWidth = 14;
            break;
          }
    
        }
    
        function draw(dot) {
          var ctx = canvas.getContext("2d");
          ctx.beginPath();
          ctx.globalCompositeOperation = globalCompositeOperation;
          if(dot){
            ctx.fillStyle = fillStyle;
            ctx.fillRect(currX, currY, 2, 2);
          } else {
            ctx.beginPath();
            ctx.moveTo(prevX, prevY);
            ctx.lineTo(currX, currY);
            ctx.strokeStyle = fillStyle;
            ctx.lineWidth = lineWidth;
            ctx.stroke();
          }
          ctx.closePath();
        }
    
        function erase() {
          if (confirm("Want to clear")) {
            var ctx = canvas.getContext("2d");
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            document.getElementById("canvasimg").style.display = "none";
          }
        }
    
        function save() {
          canvas.style.border = "2px solid";
          canvasimg.width = canvas.width;
          canvasimg.height = canvas.height;
          var ctx2 = canvasimg.getContext("2d");
          // comment next line to save the draw only
          ctx2.drawImage(backgroundImage, 0, 0);
          ctx2.drawImage(canvas, 0, 0);
          finalImg.src = canvasimg.toDataURL();
          finalImg.style.display = "inline";
        }
    
        function handleMouseEvent(e) {
          if (e.type === 'mousedown') {
            prevX = currX;
            prevY = currY;
            currX = e.offsetX;
            currY = e.offsetY;
            mouseClicked = true;
            draw(true);
          }
          if (e.type === 'mouseup' || e.type === "mouseout") {
            mouseClicked = false;
          }
          if (e.type === 'mousemove') {
            if (mouseClicked) {
              prevX = currX;
              prevY = currY;
              currX = e.offsetX;
              currY = e.offsetY;
              draw();
            }
          }
        }
        </script>
        <body onload="init()">
          <canvas id="can" width="400" height="400" style="position:absolute;top:10%;left:10%;border:2px solid;">
          </canvas>
          <div style="position:absolute;top:12%;left:43%;">Choose Color</div>
          <div style="position:absolute;top:15%;left:45%;width:10px;height:10px;background:green;" data-color="green" onclick="getColor(this)"></div>
          <div style="position:absolute;top:15%;left:46%;width:10px;height:10px;background:blue;" data-color="blue" onclick="getColor(this)"></div>
          <div style="position:absolute;top:15%;left:47%;width:10px;height:10px;background:red;" data-color="red" onclick="getColor(this)"></div>
          <div style="position:absolute;top:17%;left:45%;width:10px;height:10px;background:yellow;" data-color="yellow" onclick="getColor(this)"></div>
          <div style="position:absolute;top:17%;left:46%;width:10px;height:10px;background:orange;" data-color="orange" onclick="getColor(this)"></div>
          <div style="position:absolute;top:17%;left:47%;width:10px;height:10px;background:black;" data-color="black" onclick="getColor(this)"></div>
          <div style="position:absolute;top:20%;left:43%;">Eraser</div>
          <div style="position:absolute;top:22%;left:45%;width:15px;height:15px;background:white;border:2px solid;" data-color="eraser" onclick="getColor(this)"></div>
          <canvas id="canvasimg" style="display:none;" ></canvas>
          <img id="finalImg" style="position:absolute;top:10%;left:52%;display:none;" >
          <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;">
          <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;">
        </body>
        </html>