Clear canvas context without knowing the canvas dimensions

Tomáš Zato - Reinstate Monica

To clear a canvas to transparency, you do this:

var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,  //Starting coordinates
           canvas.width, canvas.height //Ending coordinates
);

But I'm writing a more complex abstract system. To avoid calling .getContext all over the place, I have a pseudo-class to manage contexts of all layers. Every sub-program is then passed a ctx variable without the canvas:

Something.prototype.renderMe = function(ctx) {
    //draw Something
}

I occurred to need to clear all context area. Is that possible just with canvas context?

markE

The context contains a property which refers to its canvas.

var myCanvas=ctx.canvas;
ctx.clearRect(0,0,myCanvas.width,myCanvas.height);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to apply ColorMatrix along with Canvas Context

분류에서Dev

Draw multiple shapes on canvas with mouse - clearrect doesnt clear on fast moves

분류에서Dev

Insert canvas context to html string in new dynamically created window html

분류에서Dev

Canvas Context와 함께 ColorMatrix를 적용하는 방법

분류에서Dev

video on canvas keeps resizing

분류에서Dev

Canvas drawImage size error

분류에서Dev

Adding (BezierSegment to a) Path to Canvas

분류에서Dev

changing images on a canvas with transitions

분류에서Dev

Android Canvas onTouch

분류에서Dev

Use animation images with canvas

분류에서Dev

Canvas Spinning Cube

분류에서Dev

Mask image in html canvas

분류에서Dev

Gradient in HTML Canvas

분류에서Dev

Saving Tkinter Canvas object

분류에서Dev

Inserting Canvas into an img tag

분류에서Dev

Android not drawing anything on canvas

분류에서Dev

put canvas in custom div?

분류에서Dev

Canvas same size as parent

분류에서Dev

Rendering concentric hexes on Canvas

분류에서Dev

Rendering concentric hexes on Canvas

분류에서Dev

Canvas context.drawimage가 올바른 좌표에 그려지지 않습니다.

분류에서Dev

how can I redraw a circle on an HTML5 canvas in this specific case? (method call versus directly using context object)

분류에서Dev

WPF Clear Region on a Drawing Context?

분류에서Dev

How to set canvas width to width of canvas objects/text

분류에서Dev

Why does this canvas keep growing? (dynamic resizing of canvas)

분류에서Dev

HTML Canvas 총 기능

분류에서Dev

Circle moving around the edge of the canvas

분류에서Dev

Improving Canvas draw call Performance

분류에서Dev

Low framerate writing bitmaps to the canvas