JS:飞行随机物(图像)

时间:2016-11-22 12:50:06

标签: javascript css html5 animation google-adwords

我对我在photoshop中的概念提出了一个小问题:

enter image description here

我需要创建html5 / css / js adwords banner,它们会将背景图像(红色五边形)悬浮在整个横幅的最前端,因为除了那些悬浮图片外,还会有其他对象(文本,按钮)。

有可能实现这一点,如果用户将光标移到该横幅上,也可以改变在光标位置移动这些红色五边形基座的路径吗?

编辑: 我找到了这个项目:https://codepen.io/VIRU/pen/FAdkl 可以编辑飞行图片吗?

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, 'rgb(19, 105, 168)');
        grad.addColorStop(1, 'rgb(0, 0, 0)');

        //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);

} 

1 个答案:

答案 0 :(得分:0)

http://codepen.io/zimon/pen/KNmKpN

尝试更改绘制到图像的圆圈。

var img = new Image();
    img.src= 'http://www.media3.net/images/redsquare.png';

    ctx.drawImage(img,c.x,c.y);