在尝试了复合操作并在画布上绘制图像之后,我现在尝试删除图像并进行合成。我该怎么做呢?
我需要清除画布才能重画其他图像;这可能会持续一段时间,所以我认为每次绘制一个新矩形都不是最有效的选择。
在尝试了复合操作并在画布上绘制图像之后,我现在尝试删除图像并进行合成。我该怎么做呢?
我需要清除画布才能重画其他图像;这可能会持续一段时间,所以我认为每次绘制一个新矩形都不是最有效的选择。
fastest way:
canvas = document.getElementById("canvas");
c = canvas.getContext("2d");
//... some drawing here
i = c.createImageData(canvas.width, canvas.height);
c.putImageData(i, 0, 0); // clear context by putting empty image data
I always use
cxt.fillStyle = "rgb(255, 255, 255)";
cxt.fillRect(0, 0, canvas.width, canvas.height);
the shortest way:
canvas.width += 0
function clear(context, color)
{
var tmp = context.fillStyle;
context.fillStyle = color;
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.fillStyle = tmp;
}
Context.clearRect(starting width, starting height, ending width, ending height);
Example: context.clearRect(0, 0, canvas.width, canvas.height);
I have found that in all browsers I test, the fastest way is to actually fillRect with white, or whataever color you would like. I have a very large monitor and in full screen mode the clearRect is agonizingly slow, but the fillRect is reasonable.
context.fillStyle = "#ffffff";
context.fillRect(0,0,canvas.width, canvas.height);
The drawback is that the canvas is no longer transparent.
in webkit you need to set the width to a different value, then you can set it back to the initial value
Use clearRect method by passing x,y co-ordinates and height and width of canvas. ClearRect will clear whole canvas as :
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
A quick way is to do
canvas.width = canvas.width
Idk how it works but it does!
This is 2018 and still there is no native method to completely clear canvas for redrawing. clearRect()
does not clear the canvas completely. Non-fill type drawings are not cleared out (eg. rect()
)
1.To completely clear canvas irrespective of how you draw:
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
Pros: Preserves strokeStyle, fillStyle etc.; No lag;
Cons: Unnecessary if you are already using beginPath before drawing anything
2.Using the width/height hack:
context.canvas.width = context.canvas.width;
OR
context.canvas.height = context.canvas.height;
Pros: Works with IE Cons: Resets strokeStyle, fillStyle to black; Laggy;
I was wondering why a native solution does not exist. Actually, clearRect()
is considered as the single line solution because most users do beginPath()
before drawing any new path. Though beginPath is only to be used while drawing lines and not closed path like rect().
This is the reason why the accepted answer did not solve my problem and I ended up wasting hours trying different hacks. Curse you mozilla
These are all great examples of how you clear a standard canvas, but if you are using paperjs, then this will work:
Define a global variable in JavaScript:
From your PaperScript define:
Now wherever you set clearCanvas to true, it will clear all the items from the screen.