HTML canvas spotlight effect

前端 未结 3 1071
遇见更好的自我
遇见更好的自我 2020-12-03 23:56

Let\'s say I have the following code.

3条回答
  •  甜味超标
    2020-12-04 00:09

    this code works for me:

    x = event.pageX;
    y = event.pageY;
    radius = 10;
    context = canvas.getContext("2d");
    
    context.fillStyle = "black";
    context.fillRect(0, 0, context.canvas.width, context.canvas.height);
    
    context.beginPath();
    var radialGradient= context.createRadialGradient(x,y,1,x,y,radius);
    radialGradient.addColorStop(0,"rgba(255,255,255,1");
    radialGradient.addColorStop(1,"rgba(0,0,0,1)");
    //context.globalCompositeOperation = "destination-out";
    context.fillStyle = radialGradient; 
    context.arc(x, y, radius, 0, Math.PI*2, false);
    context.fill();
    context.closePath();
    

    it seems that this line was messing it context.globalCompositeOperation = "destination-out";

    there were also pointless lines in your code like beginnig path before filling rect and fill() function after filling path

提交回复
热议问题