【问题标题】:Canvas flying blurred circles画布飞行模糊圆圈
【发布时间】:2014-12-12 18:55:02
【问题描述】:

好吧,我从来没有处理过画布,所以我完全迷路了。尝试将此效果设置为背景:

http://codepen.io/VIRU/pen/FAdkl 

但试图让那些圈子变得模糊,我看到了这个:

http://stackoverflow.com/questions/5475755/how-to-draw-a-blurry-circle-on-html5-canvas  

但是当我尝试它时,它会弄乱整个画布并且永远不会修改实际的圆圈。所以不知道如何使它工作。

window.onload = function() {

//Create canvas and initialize it's context
var canvas = document.getElementById("flying-bubbles");
var ctx = canvas.getContext("2d");

//Set the dimensions of canvas equal to the window's dimensions
var W = window.innerWidth, H = window.innerHeight;
canvas.width = W;
canvas.height = H;

//Create an array of circles
var circles = []; 
for(var i = 0; i < 20; i++ ){
    circles.push(new create_circle());
}

//Function to create circles with different positions and velocities
function create_circle() {
    //Random Position
    this.x = Math.random()*W;
    this.y = Math.random()*H;

    //Random Velocities
    this.vx = 0.1+Math.random()*1;
    this.vy = -this.vx;

    //Random Radius
    this.r = 10 + Math.random()*50;
}

//Function to draw the background
function draw() {
    //Create the gradient
    var grad = ctx.createLinearGradient(0, 0, W, H);
    grad.addColorStop(0, 'rgba(19, 105, 168,0.8)');
    grad.addColorStop(1, 'rgba(0, 0, 0,1)');

    //Fill the canvas with the gradient
    ctx.globalCompositeOperation = "source-over";
    ctx.fillStyle = grad;
    ctx.fillRect(0,0,W,H);

    //Fill the canvas with the circles
    for(var j = 0; j < circles.length; j++) {
        var c = circles[j];

        //Draw the circle and it with the blur grad
        ctx.beginPath();
        ctx.globalCompositeOperation = "lighter";       
        ctx.fillStyle = grad;
        ctx.arc(c.x, c.y, c.r, Math.PI*2, false);
        ctx.fill();

        //Lets use the velocity now
        c.x += c.vx;
        c.y += c.vy;

        //To prevent the circles from moving out of the canvas
        if(c.x < -50) c.x = W+50;
        if(c.y < -50) c.y = H+50;
        if(c.x > W+50) c.x = -50;
        if(c.y > H+50) c.y = -50;
    }
}

setInterval(draw, 25);

} 

【问题讨论】:

    标签: javascript canvas


    【解决方案1】:

    您可以为弧线添加阴影:

    function draw() {
    
        ctx.shadowColor='rgb(19,105,168)';
        ctx.shadowBlur=20;
    
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-25
      • 2017-03-16
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多