【 HTML5画布漂浮分子 让化学动起来!】(1),【最新】

29 阅读2分钟

打开全栈工匠技能包-1小时轻松掌握SSR

两小时精通jq+bs插件开发

生产环境下如歌部署Node.js

网易内部VUE自定义插件库NPM集成

谁说前端不用懂安全,XSS跨站脚本的危害

webpack的loader到底是什么样的?两小时带你写一个自己loader

开源分享:docs.qq.com/doc/DSmRnRG…

在这里插入图片描述

代码


style.css

body{

margin: 0;

padding: 0;

overflow: hidden;

background: #2d9b95;

background: -moz-radial-gradient(center, ellipse cover, #2d9b95 0%, #0e1329 100%);

background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,#2d9b95), color-stop(100%,#0e1329));

background: -webkit-radial-gradient(center, ellipse cover, #2d9b95 0%,#0e1329 100%);

background: -o-radial-gradient(center, ellipse cover, #2d9b95 0%,#0e1329 100%);

background: -ms-radial-gradient(center, ellipse cover, #2d9b95 0%,#0e1329 100%);

background: radial-gradient(ellipse at center, #2d9b95 0%,#0e1329 100%);

filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2d9b95', endColorstr='#0e1329',GradientType=1 );

}

index.js

window.requestAnimFrame = (function(){

return window.requestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.mozRequestAnimationFrame ||

window.oRequestAnimationFrame ||

window.msRequestAnimationFrame ||

function( callback ){

window.setTimeout(callback, 1000 / 60);

};

})();

var canvas = document.getElementsByTagName("canvas")[0];

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

var w = $(document).width();

var h = $(document).height();

canvas.width = w;

canvas.height = h;

var mols = [];

function init(){

for(var i=0;i<18;i++){

var mol = new generate_mol("C8H10N4O2");

mols.push(mol);

var mol = new generate_mol("C6H6O");

mols.push(mol);

var mol = new generate_mol("C6H6");

mols.push(mol);

}

}

function draw(){

canvas.width = canvas.width;

for(var i=0;i<mols.length;i++){

var m = mols[i];

m.x += m.vx;

if(m.x >= w-100 || m.x <= 0){

m.vx = -m.vx;

}

m.y += m.vy;

if(m.y >= h-100 || m.y <= 0){

m.vy = -m.vy;

}

m.r += 0.005;

m.draw();

}

}

function generate_mol(mol){

this.x = Math.random()*w;

this.y = Math.random()*h;

this.vx = Math.random()*-2;

this.vy = Math.random()*2;

this.vr = 0.1;

this.r = Math.random()*Math.PI;

this.draw = function(){

if(mol == "C6H6O"){

//Phenol

ctx.save();

ctx.translate(this.x+20,this.y+80);

ctx.rotate(this.r);

ctx.translate(-this.x+20,-this.y-80);

ctx.beginPath();

ctx.moveTo(this.x,this.y + 5);

ctx.lineTo(this.x,this.y + 30);

ctx.lineTo(this.x - 26,this.y + 45);

ctx.lineTo(this.x - 26,this.y + 75);

ctx.lineTo(this.x,this.y + 90);

ctx.lineTo(this.x + 26,this.y + 75);

ctx.lineTo(this.x + 26,this.y + 45);

ctx.lineTo(this.x,this.y + 30);

ctx.moveTo(this.x - 20,this.y + 47);

ctx.lineTo(this.x - 20,this.y + 73);

ctx.moveTo(this.x,this.y + 83);

ctx.lineTo(this.x + 22,this.y + 70);

ctx.moveTo(this.x,this.y + 36);

ctx.lineTo(this.x + 22,this.y + 49);

ctx.strokeStyle = "rgba(255,255,255,0.2)";

ctx.lineWidth = 3;

ctx.stroke();

ctx.fillStyle = "rgba(255,255,255,0.2)";

ctx.font = "15px Arial";

ctx.fillText("OH", this.x - 5, this.y);

ctx.closePath();

ctx.restore();

}

else if(mol == "C8H10N4O2"){

//Caffeine

ctx.save();

ctx.translate(this.x+20,this.y+80);

ctx.rotate(this.r);

ctx.translate(-this.x+20,-this.y-80);

ctx.beginPath();

ctx.moveTo(this.x,this.y + 5);

ctx.lineTo(this.x,this.y + 22);

ctx.moveTo(this.x-9,this.y + 35);

ctx.lineTo(this.x - 26,this.y + 45);

ctx.lineTo(this.x - 26,this.y + 75);

ctx.lineTo(this.x,this.y + 90);

ctx.lineTo(this.x + 18,this.y + 80);

ctx.moveTo(this.x + 26,this.y + 68);

ctx.lineTo(this.x + 26,this.y + 45);

ctx.lineTo(this.x + 9,this.y + 35);

ctx.moveTo(this.x - 20,this.y + 47);

ctx.lineTo(this.x - 20,this.y + 73);

ctx.moveTo(this.x + 23,this.y + 42);

ctx.lineTo(this.x + 36,this.y + 32);

ctx.moveTo(this.x + 26,this.y + 46);

ctx.lineTo(this.x + 39,this.y + 36);

ctx.moveTo(this.x + 34,this.y + 81);

ctx.lineTo(this.x + 48,this.y + 90);

ctx.moveTo(this.x - 2,this.y + 88);

ctx.lineTo(this.x - 2,this.y + 110);

ctx.moveTo(this.x + 3,this.y + 88);

ctx.lineTo(this.x + 3,this.y + 110);

ctx.moveTo(this.x - 26,this.y + 45);

ctx.lineTo(this.x - 46,this.y + 38);

ctx.moveTo(this.x - 60,this.y + 44);

ctx.lineTo(this.x - 74,this.y + 58);

ctx.lineTo(this.x - 61,this.y + 77);

ctx.moveTo(this.x - 58,this.y + 49);

最后更多分享:前端字节跳动真题解析

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】