Javascript 如何为html5画布添加喷漆工具?

Javascript 如何为html5画布添加喷漆工具?,javascript,html,canvas,Javascript,Html,Canvas,目前我已经做了一个铅笔工具的工作,并改变了铅笔的颜色,但我想添加一个喷漆工具,它可以让你画到画布上,也可以改变喷漆的颜色 //spray paint tool tools.spray = function () { var tool = this; this.started = false; this.mousedown = function (ev) { if (!tool.started) { return; } var len = 5

目前我已经做了一个铅笔工具的工作,并改变了铅笔的颜色,但我想添加一个喷漆工具,它可以让你画到画布上,也可以改变喷漆的颜色

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
这是我尝试实现的喷漆工具JavaScript的一部分,但我无法使它工作

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
//喷漆工具
tools.spray=函数(){
var工具=这个;
this.start=false;
this.mousedown=函数(ev){
如果(!tool.started){
返回;
}
var len=5+(数学随机()*5 | 0);
对于(变量i=0;i
您可以看到完整的代码

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
如果有人能帮忙,我们将不胜感激。

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
在HTML中,选项值中包含一些javascript代码。需要将其更改为:

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
<select id="dtool">
     <option value="pencil">Pencil</option>
     <option value="spray">Spray</option>
</select>

这是我的尝试。添加了以下代码:

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
  tools.spray = function(){};
  tools.spray.prototype = new tools.pencil(); 
  tools.spray.prototype.mousemove = function (ev) {
    if (tool.started) {
      context.fillStyle = CurrentColor;
      context.rect(ev._x, ev._y, 1, 1);     

      for (var i = 20; i--;) { 
        context.rect(ev._x + Math.random() * 20 - 10, 
                     ev._y + Math.random() * 20 - 10, 1, 1);
        context.fill();
      }

    }
  };

首先,我们扩展了“铅笔”工具,因为只有
mousemove
函数不同。我们通过创建pencil工具的实例并将其用作
spray
构造函数的原型来实现这一点。然后我们覆盖
mousemove
函数,如上所述

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
这里的逻辑非常简单,只要在鼠标移动时在正方形区域内创建20个点。最好像在原始代码中一样使用圆形区域,并允许用户配置一些参数,而不是使用幻数(20和rand(20)-10)。但是,为了简单起见,事实就是这样

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}

如另一个答案中所述,选项需要更改如下:

//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}
  <option value="pencil">Pencil</option>
  <option value="spray">Spray</option>

超人陷入了困境:p:)你小提琴上的铅笔似乎不起作用。使用chrome。你说的是mspaint之类的喷漆还是photoshops喷枪之类的喷漆?哦,而且在Opera中也不起作用。你发布的代码中括号似乎没有加起来。你能解释一下你添加、删除或更改了什么吗?只有代码的答案如果不是自解释的,则不鼓励使用
//spray paint tool
tools.spray = function () {

  var tool = this;
  this.started = false;

  this.mousedown = function (ev) {

    if (!tool.started) {
      return;
    }

    var len = 5 + ( Math.random() * 5 | 0 );

    for( var i = 0; i < len; ++i ) {
      context.beginPath();
      context.strokeStyle = CurrentColor;

      context.arc(
        mouseX + Math.cos( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        mouseY + Math.sin( Math.random() * Math.PI * 2 ) * radius * Math.random(),
        1,
        0, Math.PI * 2, false
      );

      context.fill();
    }
  }, 33);
}