canvas绘制篮球并使篮球滚动

<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Declarative CSS Animation Demonstration</title>
<!--js创建canvas-->
    <!--<script>-->
        <!--var canvas=document.createElement("canvas");-->
        <!--canvas.innerHTML="在支持canvas元素的浏览器中,该元素应该出现在这里.";-->
        <!--canvas.height=200;-->
        <!--canvas.width=200;-->
        <!--document.body.appendChild(canvas);//...或者你希望canvas去的其他任何地方-->
    <!--</script>-->

</head>

<body>
<canvas width="550" height="350" id="canvass">
    在支持canvas元素的浏览器中,该元素应该出现在这里

</canvas>
<script>
    window.onload = function () {
        // Pay attention to the similarities between this and the code in
        // canvas-transforms.html.
        var canvas = document.getElementById("canvass");
        /* Note how, aside from the conversion of the for loop into a
         nextFrame function, the code has not otherwise changed much
         from the transformation example.  The only other differences
         are adjustments to the values: they make smaller changes to
         accommodate the frequency with which the canvas is redrawn. */
        var renderingContext = canvas.getContext("2d");
        var xStep = 2.5, yStep = -10.0;

        // Variables to represent the absolute position, rotation, and
        // scaling of the ball.
        var x = 5, y = 300, angle = 0;
        var compression = 0.5;

        // Start the ball at the bottom-left of the canvas.
        var nextFrame = function () {
            // Always return to the same state after each iteration.
            renderingContext.save();

            // Clear the canvas.
            renderingContext.clearRect(0, 0, canvas.width, canvas.height);

            // Move the ball to the current position.
            renderingContext.translate(x, y);

            // Scale and rotate the ball.
            renderingContext.scale(1, compression);
            renderingContext.rotate(angle);

            // *Now* draw.
            drawBasketball(renderingContext);

            // Calculate the new position, rotation, and scale.
            x += xStep; y += yStep; yStep += 0.25;
            angle += Math.PI / 180; // 1 degree.
            compression += (compression <= 0.95) ? 0.05 : 0;

            // Quick check to see if the ball has hit the "floor."
            // This results in a "bounce."
            if (y + yStep > 300) {
                compression = 0.5;
                y = 300; yStep = -10.0;
            }

            // One more check to see if the ball has gone "off-canvas."
            // This moves the ball back to the left side.
            if (x > canvas.width) {
                x = 50;
            }

            renderingContext.restore();
        };

        // One hundred frames per second!
        setInterval(nextFrame, 10);
    };
    var drawBasketball = function(renderingContext) {
        renderingContext.save();
        var gradient = renderingContext.createRadialGradient(-15, -15, 5, 15, 15, 75);//createLinearGradient() 方法创建放射状/圆形渐变对象。
        gradient.addColorStop(0, "rgb(255, 130, 0)");//addColorStop() 方法规定 gradient 对象中的颜色和位置。
        gradient.addColorStop(0.75, "rgb(128, 65, 0)");
        gradient.addColorStop(1, "rgb(62, 32, 0)");
        renderingContext.fillStyle = gradient;

        renderingContext.beginPath();
        renderingContext.arc(0, 0, 50, 0, 2 * Math.PI, true);
        renderingContext.fill();

        renderingContext.strokeStyle = "blue";
        renderingContext.lineWidth = 1;
        renderingContext.beginPath();
        renderingContext.moveTo(0, -49);
        renderingContext.bezierCurveTo(30, -35, 30, 35, 0, 49);
        renderingContext.moveTo(-49, 0);
        renderingContext.bezierCurveTo(-35, -30, 35, -30, 47, -15);
        renderingContext.moveTo(-35, 35);
        renderingContext.bezierCurveTo(0, -30, 50, -20, 45, 20);
        renderingContext.moveTo(-28, -40);
        renderingContext.bezierCurveTo(10, -35, 25, -35, 29, -40);
        renderingContext.stroke();
        renderingContext.restore();
    };

</script>
</body>
</html>

使用WebGL实现3D图形  http://javascript.cs.lmu.edu/webgl-sierpinski/

转载于:https://my.oschina.net/u/3161974/blog/861519

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值