html5画布包括颜色选择

时间:2017-02-18 18:35:18

标签: javascript jquery html5 canvas

我已经制作了一个画布,您可以自由地绘制它。 但我无法添加一些颜色,取决于用户的选择。

我在网上发现了这个来源,但我不知道如何用我的代码管理它 - > http://intridea.github.io/sketch.js/

我想提供颜色选择:Sample

到目前为止,这是我的代码:

 $(function() {
    $.each(['#f00', '#ff0', '#0f0'], function() {
      $('#colors_demo .tools a').append("<a href='#displaycake_doodle' data-color='" + this + "' style='width: 10px; background: " + this + ";'></a> ");
    });
    
    $(function() {
        var drawObject = $('#displaycake_doodle').sketch();
        var oldRedraw = drawObject.data('sketch').redraw;
            drawObject.data('sketch').redraw = function() {
            oldRedraw.call(this);
          display();
         }
        $(".tools a").eq(0).attr("style", "color:#fba557");
        $(".tools a").click(function() {
            $(".tools a").removeAttr("style");
            $(this).attr("style", "color:#000"); //default color black, how do i change this here?
          });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="http://intridea.github.io/sketch.js/lib/sketch.js"></script>

<canvas id="displaycake_doodle"></canvas>

<div class="demo" id="colors_demo">
 <div class="tools">

<a href="#displaycake_doodle" class="btn btn-primary" data-tool="marker"> Marker</a> <!--works fine -->
<a href="#displaycake_doodle" class="btn btn-primary" data-tool="eraser"> Eraser</a> <!-- works fine -->

<!-- COLOR PART THAT DOESN'T SHOWS UP AND CREATES AN ERROR -->
<a href="#displaycake_doodle" data-color="#f00" style="width: 10px; background: #f00;"></a>
<a href="#displaycake_doodle" data-color="#ff0" style="width: 10px; background: #ff0;"></a>
<a href="#displaycake_doodle" data-color="#0f0" style="width: 10px; background: #0f0;"></a>

  </div>

                          </div>

当我包含这些颜色时,我收到了错误, 希望你能帮助我。非常感谢!!!

1 个答案:

答案 0 :(得分:1)

经过sketch.js之后。我正在尝试重新创建你的画布。

&#13;
&#13;
$(function() {
    $.each(['#f00', '#ff0', '#0f0'], function() {
    
      $('#colors_demo').append("<a href='#colors_sketch' data-color='" + this + "' style='width: 30px;height: 30px;display:inline-block; background: " + this + ";'></a> ");
    });
    $('#colors_sketch').sketch();
    $('#colors_sketch').sketch({defaultColor: "#ff0"});
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="http://intridea.github.io/sketch.js/lib/sketch.js"></script>

<div id="colors_demo" class="tools">
  <a href="#colors_sketch" data-download="png" style="float: right; width: 100px;">Download</a>
</div>
<div class="tools">
  <a href="#colors_sketch" data-tool="marker">Marker</a>
  <a href="#colors_sketch" data-tool="eraser">Eraser</a>
</div>
<canvas id="colors_sketch" width="800" height="300"></canvas>
&#13;
&#13;
&#13;

希望这有帮助