简体   繁体   English

HTML5画布擦除线

[英]HTML5 Canvas erase lines

I'm not familiar with jquery and canvas, I would like to make an eraser (like the eraser tool in photoshops or paint) , erases some lines in my canvas. 我对jquery和canvas不熟悉,我想制作一个橡皮擦(如photoshop或paint中的橡皮擦工具),擦除画布中的某些线条。

Marker - triggers to start draw 标记-触发开始绘制
Reset - clears the canvas Eraser - erases unnecessary lines/sketch (What i would like to do) 重置-清除画布橡皮擦-擦除不必要的线条/素描(我想做的事)

Here's the code so far I got using different sources, I would like to include ERASER 到目前为止,这是我使用不同来源的代码,我想包括ERASER

 $(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"}); }); 
 <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"> </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> 

I was having difficulty to search online since it just shows reset or undo Hope somebody can help me out. 我很难在线搜索,因为它只显示resetundo希望有人可以帮助我。 THANK YOU SO MUCH!!!! 非常感谢!!!!

Why dont you get around and use the background color as an eraser tool 为什么不四处走走并使用背景色作为橡皮擦工具

Below a sample sniipet : 在样本片段下方:

 $(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> "); }); var color = getBackground($('#colors_sketch')); //console.log(color); $("#eraser").attr('data-color',color); $('#colors_sketch').sketch(); $('#colors_sketch').sketch({defaultColor: "#ff0"}); }); function getBackground(jqueryElement) { var color = jqueryElement.css("backgroundColor"); if(color == "transparent"){ color = jqueryElement.parent().css("backgroundColor") == "transparent" ? "#fff" : jqueryElement.parent().css("backgroundColor"); //alert(color) } return hexc(color); } function hexc(colorval) { var parts = colorval.match(/^rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)$/); delete(parts[0]); for (var i = 1; i <= 3; ++i) { parts[i] = parseInt(parts[i]).toString(16); if (parts[i].length == 1) parts[i] = '0' + parts[i]; } color = '#' + parts.join(''); return color; } 
 #colors_sketch { border:1px solid black; background-color:#999; } 
 <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"> </div> <div class="tools"> <a href="#colors_sketch" data-tool="marker">Marker</a> <a id="eraser" href='#colors_sketch' data-color=''>Eraser</a> <a href="#colors_sketch" data-tool="eraser">Clear</a> </div> <canvas id="colors_sketch" width="800" height="300"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM