首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML画布浮动效果

HTML画布浮动效果
EN

Stack Overflow用户
提问于 2017-06-06 16:01:28
回答 1查看 435关注 0票数 1

可能的修正(效率/性能问题随着时间的推移?)

代码语言:javascript
复制
let i = 0; 
function float() { 
    yPos += Math.sin(i); 
    i += .01 * yVel; 
} 

我试图在HTML5画布中重新创建“浮动”效果。我想要做的是让一个图像“浮动”上下。绘制图像,运行float()函数来计算图像的下一个y位置。我已经让它浮动下来,或向上,但不是如何改变后,图像浮动的例子100 to (向上或向下)的方向。我该怎么做?

我在这里上传了画布:https://stuff.darkleaks.net/HTML5%20WP/cheshire/

屁股,你可以看到,图像漂浮下来,并放缓。在它停止移动后,我想改变浮动方向。float()函数就是我尝试这样做的地方。

代码语言:javascript
复制
   // Request animation frame
    const requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

    // Canvas
    const c = document.getElementById('canvas');
    const ctx = c.getContext('2d');

    // Set full-screen
    c.width = window.innerWidth;
    c.height = window.innerHeight;

    // Framerate settings
    // Better not touch theese if you
    // do not know what you are doing
    let fps = (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) ? 29 : 60;
    let now, delta;
    let then = Date.now();
    const interval = 1000 / fps;

    // Preparation below this comment line
    const background = new Image();
    const cheshire = new Image();
    const images = [background, cheshire];
    var imagesCount = images.length;

    background.onload = cheshire.onload;
    background.src = 'bg.png';
    cheshire.src = 'cheshire.png';

    let originalXPos = c.width / 2 - images[1].width / 2;
    let originalYPos = c.height / 2 - images[1].height / 2;
    let xPos = c.width / 2 - images[1].width / 2;
    let yPos = c.height / 2 - images[1].height / 2;
    let xVel = 0;
    let yVel = .5;

    // Draw
    function draw() {

        // Loop
        requestAnimationFrame(draw);
        now = Date.now();
        delta = now - then;

        if (delta > interval) {

            // Calculate then
            then = now - (delta % interval);

            // All your own stuff here
            drawImages();
            float();

        }

    }

    // Draw background
    function drawImages() {
        ctx.drawImage(images[0], 0, 0)
        ctx.drawImage(images[1], xPos, yPos);
    }

    function float() {

        yPos += 1 / yVel;
        yVel += .05;

        // IF the image has floated 100 px (up or down)
        // change direction

    }

    // Returns an random integer between
    // min and max
    function randInt(min, max) {
        max = max === undefined ? min - (min = 0) : max;
        return Math.floor(Math.random() * (max - min) + min);
    }

    // Cursor coordinates X & Y
    function clicked(e) {

        let x, y;

        if (e.offsetX) {
            x = e.offsetX;
            y = e.offsetY;
        } else if (e.layerX) {
            x = e.layerX;
            y = e.layerY;
        }

        // Do something...

    }

    $('canvas').on('click', function(e) {
        clicked(e);
    });

    requestAnimationFrame(draw);nter code here
EN

回答 1

Stack Overflow用户

发布于 2017-06-06 19:24:18

如问题中所示,在代码中向下浮动。

代码语言:javascript
复制
// in the draw function
xPos += xVel;
yPos += yVel;

然后检查yPos是否经过某个点,如果是的话,改变方向

代码语言:javascript
复制
const bottom = ctx.canvas.height - 300; // put somewhere with globals 
if(yPos > bottom){ // is pos past bottom
   yVel = -0.5; // change direction
   yPos = bottom; // make sure yPos does not render past bottom
}

顶上也一样

代码语言:javascript
复制
const top = 100; // put somewhere with globals 
if(yPos < top){ // is pos past top
   yVel = 0.5; // change direction
   yPos = top; // make sure yPos does not render past top
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44394801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档