G
G
Gasoid2013-12-11 22:58:04
Canvas
Gasoid, 2013-12-11 22:58:04

Is it possible to "erase" an area in the canvas so that the page is visible in the background?

Is it possible to "erase" an area in the canvas so that the page is visible in the background?
Here is an example:

<!doctype html>
<html lang="ru">

<head>
    <meta charset="utf-8" />
    <title>
        Example
    </title>
    <script src="http://yandex.st/jquery/2.0.3/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            var canvas = document.getElementById('area');
            var ctx = canvas.getContext("2d");
            ctx.fillStyle = "rgba(255,255,255, 0.5)";
            ctx.fillRect(0, 0, $("#area").width(), $("#area").height());
            console.log($("#area").width());
            $(canvas).bind('mousedown', function (e) {
                eraser(e, ctx, 40);
                $(canvas).bind('mousemove', function (e) {
                    eraser(e, ctx, 40);
                });
            });

            $(canvas).bind('mouseup', function () {
                $(canvas).unbind('mousemove');
            });

        });

        function eraser(e, context, radius) {
            var mouseX, mouseY;
            //console.log(e.pageX);
            mouseX = e.pageX;
            mouseY = e.pageY;

            context.clearRect(mouseX, mouseY, radius, radius);

        }
    </script>
</head>

<body>
    <div id="wrapper">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip
            ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril
            delenit augue duis dolore te feugait nulla facilisi.</p>
    </div>
    <canvas id="area" style="width: 100%;height: 100%;position:fixed;top:0;left:0;"></canvas>
</body>

</html>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
itspers, 2013-12-12
@Gasoid

Too little jquery)
I threw out the jQuery, removed the documentready, added a canvas resize, slightly corrected it so that it would draw in the right place - then I looked at the source code - it seemed like it should have worked like that.
So xs why didn't work, but anyway:
jsfiddle.net/itspers/Sjcch/3

var canvas_down = false;  

var canvas = document.getElementById('area');
var ctx = canvas.getContext("2d");

//resize to be sure that pixels == width
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;   
}
resizeCanvas();


ctx.fillStyle = "rgba(255,255,255, 0.9)";
ctx.fillRect(0, 0, $("#area").width(),$("#area").height()); 

canvas.addEventListener('mousedown', function (e) {
    canvas_down = true;    
}, false);

canvas.addEventListener('mouseup', function (e) {
    canvas_down = false;    
}, false);

canvas.addEventListener('mousemove', function (e) {
    if (canvas_down)
        eraser(e, 25);    
}, false);

   

function eraser(e, radius) {

    ctx.beginPath();
    var x2 = e.pageX - (radius/2);            
    var y2 = e.pageY- (radius/2);    
    ctx.clearRect(x2, y2, radius, radius);             
    ctx.closePath();

}

O
Oleg Kolesnikov, 2013-12-11
@DOC_tr

No. You can make (in this case) 4 blocks that will frame the transparent area, and create the effect that the area is cut out.

G
GreatRash, 2013-12-12
@GreatRash

If you only care about the latest versions of browsers (IE11) and don’t care about mobile devices (namely Opera Mini and IE10 Mobile), then you can erase part of the image from the canvas using clearRect(), and read about pointer-events in addition .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question