简体   繁体   English

如何清除Canvas中的特定行:HTML5

[英]How to clear specific line in Canvas : HTML5

I am totally new to this Canvas Element. 我对这个Canvas元素完全陌生。 I am able to draw line in canvas, but not able to clear only specif line. 我能够在画布上绘制线条,但不能仅清除特定线条。 Whole canvas become blank. 整个画布变成空白。

Tried this: HTML: 试过这个: HTML:

<canvas id="cvs" width="400" height="400"></canvas>
<hr />
<input type="submit" id="reDrowA" value="Draw A" />
<input type="submit" id="reDrowB" value="Draw B" />
<hr />
<input type="submit" id="clearA" value="Clear A" />
<input type="submit" id="clearB" value="Clear B" />

Script 脚本

$(document).ready(function(){
    canvas =  document.getElementById("cvs");    
    $("#reDrowA").on("click",function(){
        a = canvas.getContext('2d');
        a.translate(0.5, 0.5);
        a.beginPath();
        a.setLineDash([2,10]);
        a.moveTo(10,10);
        a.lineTo(300,10);
        a.lineTo(300,300);
        a.stroke();
    });
    $("#reDrowB").on("click",function(){
        b = canvas.getContext('2d');
        b.translate(0.5, 0.5);
        b.beginPath();
        b.setLineDash([2,10]);
        b.moveTo(10,10);
        b.lineTo(10,300);
        b.lineTo(300,300);
        b.stroke();
    });
    $("#clearA").on("click",function(){
       a.clearRect(0, 0, canvas.width, canvas.height);
    });
    $("#clearB").on("click",function(){
        b.clearRect(0, 0, canvas.width, canvas.height);
    });

});

Fiddle: http://jsfiddle.net/8YNvu/ 小提琴: http//jsfiddle.net/8YNvu/

About Canvas, Canvas 'elements', and the visibility of `elements' ... 关于Canvas,Canvas'元素',以及`elements'的可见性......

When any element on the canvas needs to change (move, erase, etc), the standard method is to completely erase the canvas and redraw the canvas with the elements in their new positions (or not redraw the elements if they are erased). 当画布上的任何元素需要更改(移动,擦除等)时,标准方法是完全擦除画布并使用新位置中的元素重绘画布(或者如果元素被删除则不重绘元素)。

That's because canvas does not "remember" where it drew any individual element and therefore cannot individually move or erase any element. 这是因为画布不“记住”它绘制任何单个元素的位置,因此无法单独移动或擦除任何元素。

It's up to you to "remember" enough information about an element to redraw it after the canvas has been erased. 在画布被删除后,您需要“记住”有关要重绘的元素的足够信息。

A Demo: http://jsfiddle.net/m1erickson/Wrk2e/ 演示: http//jsfiddle.net/m1erickson/Wrk2e/

So in your example you could create javascript objects a and b to represent your top-right and left-bottom line paths. 因此,在您的示例中,您可以创建javascript对象ab来表示您的右上角和左下角线路径。

Each object would have the points which define its line-path and a flag indicating if it is visible (visible == redrawn on the canvas). 每个对象都有定义其行路径的点和一个标志,指示它是否可见(在画布上可见==重绘)。

// create an object containing the top-right lines
// the object contains its path points & if it is visible or not
var a={
  path:[10,10, 300,10, 300,300],
  isVisible:false,
}

// create an object containing the left-bottom lines
// the object contains its path points & if it is visible or not
var b={
  path:[10,10, 10,300, 300,300],
  isVisible:false,
}

For easy processing you can put all your line-path objects in an array: 为了便于处理,您可以将所有行路径对象放在一个数组中:

// an array containing all the line-path objects
var myObjects=[a,b];

Then when you clear the canvas you simply use each objects line-path information to redraw the line. 然后,当您清除画布时,只需使用每个对象的线路径信息来重绘该线条。 If a particular objects visibility flag is false then don't redraw that particular object. 如果特定对象可见性标志为false则不要重绘该特定对象。

// clear the entire canvas 
// redraw any line-paths that are visible
function redrawAll(myObjects){
    context.clearRect(0,0,canvas.width,canvas.height);
    for(var i=0;i<myObjects.length;i++){
        if(myObjects[i].isVisible){
            drawLinePath(myObjects[i]);
        }
    }
}

// redraw 1 line-path
function drawLinePath(theObject){
    var points=theObject.path;
    // save the current untranslated context state
    context.save();

    // draw lines through each point in the objects path
    context.translate(0.5, 0.5);
    context.beginPath();
    context.setLineDash([2,10]);
    context.moveTo(points[0],points[1]);
    for(var i=2;i<points.length;i+=2){
        context.lineTo(points[i],points[i+1]);
    }
    context.stroke();

    // restore the context to its untranslated state
    context.restore();
}

With all this in place, your buttons simply change the visibility flag on a particular line-path object and then clear/redraw the entire canvas. 完成所有这些后,您的按钮只需更改特定线路径对象上的可见性标记,然后清除/重绘整个画布。

// use buttons to set & clear the visibility flags on objects
// In all cases, clear the entire canvas and redraw any visible objects

$("#reDrowA").on("click",function(){
    a.isVisible=true;
    redrawAll(myObjects);
});
$("#reDrowB").on("click",function(){
    b.isVisible=true;
    redrawAll(myObjects);
});
$("#clearA").on("click",function(){
    a.isVisible=false;
    redrawAll(myObjects);
});
$("#clearB").on("click",function(){
    b.isVisible=false;
    redrawAll(myObjects);
});

Canvas is transparent. 画布是透明的。 It is not possible to acheive in single canvas tag . acheive in single canvas tag不可能实现。 because the clearRect functionality to clear based on width and height. 因为clearRect功能可以根据宽度和高度进行清除。 we didn't give the exact position to clear the canvas. 我们没有给出清除画布的确切位置。 Try the fiddle . 试试小提琴。 you acheive the scenario with two canvas tags. 你使用两个canvas标签来实现场景。

Fiddle 小提琴

You just have to re-paint the lines that should persist after you clear the Canvas. 您只需re-paint the lines清除Canvas后应该保留re-paint the lines
Maybe like this: http://jsfiddle.net/8YNvu/10/ 也许是这样的: http//jsfiddle.net/8YNvu/10/

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

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