Canvas 入门5 在Canvas中使用HTML元素

本文学习资源来自《HTML5 Canvas核心技术 图形、动画与游戏开发》

为了让HTML控件放到canvas范围内,可以使用CSS将这些控件放置在canvas之上。
示例:

<html>
   <head>
      <title>Bouncing Balls</title>

      <style> 
         body {
            background: #dddddd;
         }

         #canvas {
            margin-left: 10px;
            margin-top: 10px;
            background: #ffffff;
            border: thin solid #aaaaaa;
         }

         #glasspane {
            position: absolute;
            left: 50px;
            top: 50px;
            padding: 0px 20px 10px 10px;
            background: rgba(0, 0, 0, 0.3);
            border: thin solid rgba(0, 0, 0, 0.6);
            color: #eeeeee;
            font-family: Droid Sans, Arial, Helvetica, sans-serif;
            font-size: 12px;
            cursor: pointer;
            -webkit-box-shadow: rgba(0,0,0,0.5) 5px 5px 20px;
            -moz-box-shadow: rgba(0,0,0,0.5) 5px 5px 20px;
            box-shadow: rgba(0,0,0,0.5) 5px 5px 20px;
         }

         #glasspane h2 {
            font-weight: normal;
         }

         #glasspane .title {
            font-size: 2em;
            color: rgba(255, 255, 0, 0.8);
         }

         #glasspane a:hover {
            color: yellow;
         }

         #glasspane a {
            text-decoration: none;
            color: #cccccc;
            font-size: 3.5em;
         }

         #glasspane p {
            margin: 10px;
            color: rgba(65, 65, 220, 1.0);
            font-size: 12pt;
            font-family: Palatino, Arial, Helvetica, sans-serif;
         }
      </style>
   </head>

   <body>
      <div id='glasspane'>
         <h2 class='title'>Bouncing Balls</h2>

         <p>One hundred balls bouncing</p>

         <a id='startButton'>Start</a>
      </div>

      <canvas id='canvas' width='750' height='500'>
         Canvas not supported
      </canvas>

  </body>
</html>

下面的代码实例是在canvas 上画跳到的小球。

var context = document.getElementById('canvas').getContext('2d'),
startButton = document.getElementById('startButton'),
glasspane = document.getElementById('glasspane'),
paused = true,
circles = [];

drawGrid(context, 'lightgray', 10, 10);

context.lineWidth = 0.5;
context.font = '32pt Ariel';

//创建100个球
for (var i=0; i < 100; ++i) {
    circles[i] = { 
        x: 300, 
        y: 300, 
        velocityX: 3*Math.random(),     //x速度
        velocityY: 3*Math.random(),     //y速度
        radius: 50*Math.random(),       //半径
        color: 'rgba(' + (Math.random()*255).toFixed(0) + ', ' +
                        (Math.random()*255).toFixed(0) + ', ' +
                        (Math.random()*255).toFixed(0) + ', 1.0)' 
    };
}

startButton.onclick = function(e) {
    e.preventDefault();
    e.stopPropagation();
    paused = ! paused;
    startButton.innerText = paused ? 'Start' : 'Stop';
};

glasspane.onmousedown = function(e) {
    e.preventDefault();
    e.stopPropagation();
}

context.canvas.onmousedown = function(e) {
    e.preventDefault();
    e.stopPropagation();
};

setInterval(function() {
    if (!paused) {
        context.clearRect(0, 0, context.canvas.width, context.canvas.height);
        drawGrid(context, 'lightgray', 10, 10);

        circles.forEach(function(circle) {
            context.beginPath();
            context.arc(circle.x, circle.y, circle.radius, 0, Math.PI*2, false);
            context.fillStyle = circle.color;
            context.fill(); 
            adjustPosition(circle);
        });
    }
}, 1000 / 60);

function adjustPosition(circle) {
    //边缘检测
    if (circle.x + circle.velocityX + circle.radius > context.canvas.width ||
    circle.x + circle.velocityX - circle.radius < 0) 
        circle.velocityX = -circle.velocityX;

    //边缘检测
    if (circle.y + circle.velocityY + circle.radius > context.canvas.height ||
    circle.y + circle.velocityY - circle.radius  < 0) 
        circle.velocityY= -circle.velocityY;

    circle.x += circle.velocityX;
    circle.y += circle.velocityY;
}

function drawGrid(context, color, stepx, stepy) {
    context.strokeStyle = color;
    context.lineWidth = 0.5;

    for (var i = stepx + 0.5; i < context.canvas.width; i += stepx) {
        context.beginPath();
        context.moveTo(i, 0);
        context.lineTo(i, context.canvas.height);
        context.stroke();
    }

    for (var i = stepy + 0.5; i < context.canvas.height; i += stepy) {
        context.beginPath();
        context.moveTo(0, i);
        context.lineTo(context.canvas.width, i);
        context.stroke();
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程圈子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值