如何删除画布 html 中的点?

首先,我尝试了与该主题相关的所有问题和答案。此外,我尝试了相关问题并尝试解决但没有成功。所以请仔细阅读我的问题。


问题:在没有透明画布的情况下仅移除红点。


我只想删除 Red Dotes 而不是 Full Canvas Remove or Reload 。


const canvas = document.getElementById('canvas');

const context = canvas.getContext('2d');

    

context.beginPath();

context.arc(100, 100, 3, 0, Math.PI * 2, true); // Outer circle

context.lineWidth = 0;

context.fillStyle = "red";

context.fill();


context.beginPath();

context.arc(36, 100, 3, 0, Math.PI * 2, true); // Outer circle

context.lineWidth = 0;

context.fillStyle = "Orange";

context.fill();


context.beginPath();

context.arc(123, 100, 3, 0, Math.PI * 2, true); // Outer circle

context.lineWidth = 0;

context.fillStyle = "Green";

context.fill();


function removeRedDot(){

 // remove code

 alert('Remove Red Dot');

}

#canvas{

border:1px solid black;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h4>Approach the circle with the mouse</h4> <button onclick="removeRedDot()"> Red Remove Dot</button>

<canvas id="canvas" width=300 height=200></canvas>


心有法竹
浏览 189回答 2
2回答

德玛西亚99

x由于您在 ( , y) 位置 ( 100px, 100px) 处绘制了直径为 的红色圆圈6px,因此它所占的面积为:x&nbsp; &nbsp; &nbsp; : 100 - (6 / 2)y&nbsp; &nbsp; &nbsp; : 100 - (6 / 2)width&nbsp; : 6height : 6您可以使用该方法清除画布的一部分clearRect。context.clearRect(97, 97, 6, 6);如果您的画布有背景,您将需要清除整个画布并重新绘制除红点以外的所有内容,或者您可以调用fillRect… 假设它context.fillStyle已设置为背景色。context.fillRect(97, 97, 6, 6);在绘制之前,您必须以某种方式知道红点的绘制位置(以及它的大小)。编辑:在下面的演示之后查看我的 OOP 示例!演示const canvas = document.getElementById('canvas');const context = canvas.getContext('2d');context.beginPath();context.arc(100, 100, 3, 0, Math.PI * 2, true); // Outer circlecontext.lineWidth = 0;context.fillStyle = "red";context.fill();context.beginPath();context.arc(36, 100, 3, 0, Math.PI * 2, true); // Outer circlecontext.lineWidth = 0;context.fillStyle = "Orange";context.fill();context.beginPath();context.arc(123, 100, 3, 0, Math.PI * 2, true); // Outer circlecontext.lineWidth = 0;context.fillStyle = "Green";context.fill();function removeRedDot() {&nbsp; context.clearRect(97, 97, 6, 6);&nbsp; alert('Removed Red Dot');}#canvas {&nbsp; border: 1px solid black;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><h4>Approach the circle with the mouse</h4> <button onclick="removeRedDot()"> Red Remove Dot</button><canvas id="canvas" width=300 height=200></canvas>OOP 来拯救!更好的方法是了解画布渲染之外的红点。您可以将画布上下文包装在一个管理图层和可绘制对象的类中。const ctx = document.getElementById('canvas').getContext('2d');const main = () => {&nbsp; const canvas = new Canvas(ctx);&nbsp; const layer = canvas.addLayer();&nbsp; const circles = [&nbsp; &nbsp; new Circle({ x: 50, y: 50 }, 3, 'red'),&nbsp; &nbsp; new Circle({ x: 100, y: 100 }, 6, 'green'),&nbsp; &nbsp; new Circle({ x: 150, y: 150 }, 12, 'blue')&nbsp; ];&nbsp; layer.add(...circles);&nbsp; canvas.render();&nbsp;&nbsp;&nbsp; // After 2 second, remove the red dot and re-render.&nbsp; setTimeout(() => {&nbsp; &nbsp; alert('Removing "red" circle, and adding a "cyan" circle...');&nbsp; &nbsp; layer.remove(circles[0]);&nbsp; &nbsp; layer.add(new Circle({ x: 150, y: 50 }, 8, 'cyan'));&nbsp; &nbsp; canvas.render();&nbsp; }, 2000);};class Drawable {&nbsp; constructor(origin) {&nbsp; &nbsp; this.origin = origin;&nbsp; }&nbsp; draw(ctx) { }}class Layer {&nbsp; constructor(name) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.drawables = [];&nbsp; }&nbsp; add(...drawables) {&nbsp; &nbsp; drawables.forEach(drawable => this.drawables.push(drawable));&nbsp; }&nbsp; remove(drawableOrIndex) {&nbsp; &nbsp; if (isNaN(drawableOrIndex)) {&nbsp; &nbsp; &nbsp; drawableOrIndex = this.drawables.indexOf(drawableOrIndex);&nbsp; &nbsp; }&nbsp; &nbsp; if (drawableOrIndex > -1) {&nbsp; &nbsp; &nbsp; this.drawables.splice(drawableOrIndex, 1);&nbsp; &nbsp; }&nbsp; }&nbsp; render(ctx) {&nbsp; &nbsp; this.drawables.forEach(drawable => drawable.render(ctx));&nbsp; }}class Canvas {&nbsp; constructor(ctx) {&nbsp; &nbsp; this.ctx = ctx;&nbsp; &nbsp; this.layers = [];&nbsp; }&nbsp; addLayer(name) {&nbsp; &nbsp; const newLayer = new Layer(name || 'layer-' + this.layers.length);&nbsp; &nbsp; this.layers.push(newLayer);&nbsp; &nbsp; return newLayer;&nbsp; }&nbsp; getLayer(nameOrIndex) {&nbsp; &nbsp; return isNaN(nameOrIndex)&nbsp; &nbsp; &nbsp; ? this.layers.find(layer => layer.name === nameOrIndex)&nbsp; &nbsp; &nbsp; : this.layers[nameOrIndex];&nbsp; }&nbsp; render() {&nbsp; &nbsp; const { width, height } = this.ctx.canvas;&nbsp; &nbsp; this.ctx.clearRect(0, 0, width, height);&nbsp; &nbsp; this.layers.forEach(layer => layer.render(this.ctx));&nbsp; }}class Circle extends Drawable {&nbsp; constructor(origin, radius, color) {&nbsp; &nbsp; super(origin);&nbsp; &nbsp; this.radius = radius;&nbsp; &nbsp; this.color = color;&nbsp; }&nbsp; render(ctx) {&nbsp; &nbsp; const { x, y } = this.origin;&nbsp; &nbsp; const diameter = this.radius * 2;&nbsp; &nbsp; ctx.save();&nbsp; &nbsp; ctx.beginPath();&nbsp; &nbsp; ctx.arc(x, y, this.radius, 0, Math.PI * 2, true);&nbsp; &nbsp; ctx.lineWidth = 0;&nbsp; &nbsp; ctx.fillStyle = this.color;&nbsp; &nbsp; ctx.fill();&nbsp; &nbsp; ctx.restore();&nbsp; }}main();#canvas {&nbsp; border: 1px solid black;}<canvas id="canvas" width=300 height=200></canvas>

www说

由于现有的答案已经证明了面向对象的格言......“我要了一根香蕉,结果在丛林里遇到了一只拿着香蕉的大猩猩。”&nbsp;,我添加了这个答案来演示更简洁的 JavaScript 独特的 OO 方法。原因:与复杂性的斗争。复杂性是编码员的头号敌人,添加不必要的抽象层、复制现有行为、预测未定义的需求,都会增加复杂性。认为如果只有不到 100 行可能无关紧要,对于大型项目,额外的代码会很快加起来,每一行都是额外的错误来源。JavaScript 提供了一个简单且非常灵活的 OO 模型,强调通过特定对象构造和扩展实现的多态性。它还具有大量的编码快捷方式,可以大大减少实现行为所需的行数结果是功能几乎相同的代码减少了一半,例子使用Array原型实现层circledrawable通过将类型传递给构造函数来继承。按color, 而不是索引或引用删除const ctx = canvas.getContext("2d");const P2 = (x = 0, y = 0) => ({x,y});const Drawable = (pos, color, size = 10, type = Circle) => ({pos, size, color, ...type});const Circle = {&nbsp; &nbsp; draw(ctx) {&nbsp; &nbsp; &nbsp; &nbsp; ctx.fillStyle = this.color;&nbsp; &nbsp; &nbsp; &nbsp; ctx.beginPath();&nbsp; &nbsp; &nbsp; &nbsp; ctx.arc(this.pos.x, this.pos.y, this.size, 0, Math.PI * 2);&nbsp; &nbsp; &nbsp; &nbsp; ctx.fill();&nbsp; &nbsp; }};const drawables = Object.assign([], {&nbsp; &nbsp; &nbsp; &nbsp;draw(ctx) { for (const d of this) { d.draw(ctx) } },&nbsp; &nbsp; &nbsp; &nbsp;remove(color) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const idx = this.findIndex(d => d.color === color);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (idx > -1 && (this.splice(idx, 1)[0])) || undefined;&nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; });drawables.push(...[...document.querySelectorAll("#buttons button")].map((but, idx)=>&nbsp; &nbsp; Drawable(P2(100 + idx * 100, 50), but.dataset.color)));drawables.draw(ctx);&nbsp; &nbsp;buttons.addEventListener("click", e => {&nbsp; &nbsp; if (drawables.remove(e.target.dataset.color)) {&nbsp; &nbsp; &nbsp; &nbsp;ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height);&nbsp; &nbsp; &nbsp; &nbsp;drawables.draw(ctx);&nbsp; &nbsp; }});<canvas id="canvas" width="400" height="100"></canvas><div id="buttons">&nbsp; &nbsp; <button data-color="red">Remove Red</button>&nbsp; &nbsp; <button data-color="green">Remove Green</button>&nbsp; &nbsp; <button data-color="blue">Remove Blue</button></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript