【 HTML5畫布漂浮分子 讓化學動起來!】
效果
程式碼
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);
ctx.lineTo(this.x - 68,this.y + 59);
ctx.moveTo(this.x - 46,this.y + 82);
ctx.lineTo(this.x - 26,this.y + 73);
ctx.moveTo(this.x - 60,this.y + 86);
ctx.lineTo(this.x - 70,this.y + 100);
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("CH", this.x - 5, this.y);
ctx.fillText("3", this.x + 18, this.y+6);
ctx.fillText("N", this.x - 5, this.y+37);
ctx.fillText("O", this.x + 38, this.y+35);
ctx.fillText("N", this.x + 21, this.y+81);
ctx.fillText("CH", this.x + 50, this.y+99);
ctx.fillText("3", this.x + 72, this.y+105);
ctx.fillText("O", this.x - 5, this.y+124);
ctx.fillText("N", this.x - 59, this.y+42);
ctx.fillText("N", this.x - 59, this.y+84);
ctx.fillText("H C", this.x - 98, this.y+114);
ctx.fillText("3", this.x - 87, this.y+119);
ctx.closePath();
ctx.restore();
}
else if(mol == "C6H6"){
//Benzene
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 + 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.closePath();
ctx.restore();
}
}
}
init();
function animloop() {
draw();
requestAnimFrame(animloop);
}
animloop();
html以及全部原始檔我都已上傳到“我的資源”中,需要的朋友可以下載哦!
相關文章
- html5畫布canvasHTMLCanvas
- HTML5畫布概述HTML
- HTML5畫布: clearRect()HTML
- 利用分層優化 HTML5 畫布渲染優化HTML
- HTML5的畫布:PathsHTML
- HTML5怎樣建立畫布?HTML
- [計算化學]分子動力學筆記筆記
- CSS——讓“盒子”動起來:① 浮動CSS
- 國外發明可以做支架 能夠讓手機“漂浮”起來的資料線
- 10個出色的HTML5畫布示例HTML
- 運動APP能否讓“懶人”動起來?APP
- 搜狗分身技術再進化,讓AI合成主播“動”起來AI
- html5中canvas元素建立畫布介紹HTMLCanvas
- 讓人物在地圖上動起來地圖
- HTML5遊戲開發(五):飛機大戰之讓所有元素動起來HTML遊戲開發
- 運動類App能否讓“懶人”動起來?APP
- HTML5畫布如何設定線的樣式?HTML
- HTML5的畫布:Stroke(筆觸)和Fill(填充)HTML
- 如何讓Spring Boot 的配置動起來?Spring Boot
- HTML5 Canvas(畫布)實戰程式設計初級篇:基本介紹和基礎畫布元素HTMLCanvas程式設計
- 用SolidWorks畫一個鑽頭,新手學起來Solid
- 使用js實現圖畫人物動起來的方法JS
- Web開發者需知的15個HTML5畫布應用WebHTML
- 讓ppt中的資料動起來的方法
- 讓物體動起來,Unity的幾種移動方式Unity
- 讓Elasticsearch飛起來!——效能優化實踐乾貨Elasticsearch優化
- 讓 Elasticsearch 飛起來!——效能優化實踐乾貨Elasticsearch優化
- 神奇的 SQL 之效能優化 → 讓 SQL 飛起來SQL優化
- 未來web瀏覽技術提前體驗:10個會讓你驚歎不已的HTML5畫布(canvas)技術應用演示WebHTMLCanvas
- 利用emoji讓的 git commit 生動清晰起來GitMIT
- Cinemagraph Pro Mac(讓你的攝影作品動起來)Mac
- 呈現資訊的新方式:讓資料動起來
- iOS開發系列–讓你的應用“動”起來iOS
- HTML5畫布canvas隨機生成圓形和正方形HTMLCanvas隨機
- Aseprite+Cocos:打包畫素畫圖,匯入到cocos裡並動起來
- 幾個騷操作,讓程式碼自動學會畫畫,太好玩啦!
- Flutter 中使用 AnimatedContainer 讓你的 Widget 動起來FlutterAI
- 讓介面動起來!移動動效設計必備靈感網站網站