html半透明正方体,HTML5 Canvas 悬浮的等角立方体

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

// Set up our canvas

var canvas = document.createElement('canvas');

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

document.body.appendChild(canvas);

var ctx = canvas.getContext('2d');

// Pick out the form elements for easy access later

var x1 = document.querySelector('#x1');

var x2 = document.querySelector('#x2');

var y = document.querySelector('#y');

var color = document.querySelector('#color');

// Animation function

function draw() {

// clear the canvas

ctx.clearRect(0, 0, canvas.width, canvas.height);

// Wobble the cube using a sine wave

var wobble = Math.sin(Date.now() / 250) * window.innerHeight / 50;

// draw the cube

drawCube(

window.innerWidth / 2,

window.innerHeight / 2 + wobble + y.value / 2,

Number(x1.value),

Number(x2.value),

Number(y.value),

color.value

);

requestAnimationFrame(draw);

}

draw();

// Colour adjustment function

// Nicked from http://stackoverflow.com/questions/5560248

function shadeColor(color, percent) {

color = color.substr(1);

var num = parseInt(color, 16),

amt = Math.round(2.55 * percent),

R = (num >> 16) + amt,

G = (num >> 8 & 0x00FF) + amt,

B = (num & 0x0000FF) + amt;

return '#' + (0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000 + (G < 255 ? G < 1 ? 0 : G : 255) * 0x100 + (B < 255 ? B < 1 ? 0 : B : 255)).toString(16).slice(1);

}

// Draw a cube to the specified specs

function drawCube(x, y, wx, wy, h, color) {

ctx.beginPath();

ctx.moveTo(x, y);

ctx.lineTo(x - wx, y - wx * 0.5);

ctx.lineTo(x - wx, y - h - wx * 0.5);

ctx.lineTo(x, y - h * 1);

ctx.closePath();

ctx.fillStyle = shadeColor(color, -10);

ctx.strokeStyle = color;

ctx.stroke();

ctx.fill();

ctx.beginPath();

ctx.moveTo(x, y);

ctx.lineTo(x + wy, y - wy * 0.5);

ctx.lineTo(x + wy, y - h - wy * 0.5);

ctx.lineTo(x, y - h * 1);

ctx.closePath();

ctx.fillStyle = shadeColor(color, 10);

ctx.strokeStyle = shadeColor(color, 50);

ctx.stroke();

ctx.fill();

ctx.beginPath();

ctx.moveTo(x, y - h);

ctx.lineTo(x - wx, y - h - wx * 0.5);

ctx.lineTo(x - wx + wy, y - h - (wx * 0.5 + wy * 0.5));

ctx.lineTo(x + wy, y - h - wy * 0.5);

ctx.closePath();

ctx.fillStyle = shadeColor(color, 20);

ctx.strokeStyle = shadeColor(color, 60);

ctx.stroke();

ctx.fill();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值