日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html桌面雪花,html5 canvas雪花形状在线生成器

發布時間:2024/9/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html桌面雪花,html5 canvas雪花形状在线生成器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JavaScript

語言:

JaveScriptBabelCoffeeScript

確定

console.clear();

const canvas = document.querySelector('.stage'),

ctx = canvas.getContext('2d'),

canvasSize = 200,

midpt = {x:canvasSize * .5, y:canvasSize * .5},

maxRadius = canvasSize * .4,

mcos = Math.cos,

mmax = Math.max,

mrandom = Math.random,

mround = Math.round,

msin = Math.sin,

msqrt = Math.sqrt,

tau = Math.PI * 2,

PI180 = Math.PI/180,

fillColour = 'hsla(45, 23%, 90%, .3)',

strokeColour = 'hsla(45, 23%, 90%, .8)';

const offscreen = document.createElement('canvas'),

offscreenCtx = offscreen.getContext('2d'),

offscreenSize = 40,

paper = document.querySelector('.paper');

let sections = [];

document.body.addEventListener('click', () => animate() );

canvas.width = canvasSize;

canvas.height = canvasSize;

ctx.fillStyle = fillColour;

ctx.strokeStyle = strokeColour;

ctx.lineWidth = .5;

offscreenCtx.width = offscreenSize;

offscreenCtx.height = offscreenSize;

const Trunk = function(o, rot){

this.root = {x:o.x, y:o.y};

this.rotation = rot;

this.length = mrandom() * (maxRadius * .25) + (maxRadius * .75);

this.tip = getPolarPoint(this.length, this.rotation, this.root);

this.spurs = [];

this.spurs1 = [];

this.spurs2 = [];

this.spurDeets = [];

this.numSpurs = mround(mrandom() * 2 + 8);

}

Trunk.prototype.generate = function(){

let step = (this.length * .95) / this.numSpurs;

for(let i=0; i

let spurDeetObj = {};

spurDeetObj.step = step;

spurDeetObj.spurLength = step * i + (mrandom() * (i === this.numSpurs-1 ? 5 : 30) + 5);

spurDeetObj.spurRotation = mrandom() * 10 + (i === 0 ? 30 : 5);

this.spurDeets.push(spurDeetObj);

}

this.buildSpurs();

}

Trunk.prototype.buildSpurs = function(){

for(let i=0; i

let spurDeet = this.spurDeets[i];

let root = getPolarPoint(spurDeet.step * i, this.rotation, this.root);

this.spurs1.push(new Spur(root, getPolarPoint(spurDeet.spurLength, this.rotation + spurDeet.spurRotation, this.root)));

this.spurs2.push(new Spur(root, getPolarPoint(spurDeet.spurLength, this.rotation - spurDeet.spurRotation, this.root)));

}

this.spurs = this.spurs1.concat(this.spurs2.reverse());

}

Trunk.prototype.clone = function(){

let cloneObj = {};

cloneObj.length = this.length;

cloneObj.tip = this.tip;

cloneObj.numSpurs = this.numSpurs;

cloneObj.spurDeets = this.spurDeets;

return cloneObj;

}

Trunk.prototype.buildFromClone = function(cloneObj){

this.length = cloneObj.length;

this.numSpurs = cloneObj.numSpurs;

this.spurDeets = cloneObj.spurDeets;

this.buildSpurs();

}

Trunk.prototype.draw = function(seed = .5){

// Centre of trunk

ctx.beginPath();

ctx.moveTo(this.root.x, this.root.y);

ctx.lineTo(this.tip.x, this.tip.y);

ctx.stroke();

// Centre of spurs

for(let i=0; i

this.spurs[i].drawSpur();

}

// Edges of spur

ctx.moveTo(this.spurs[0].root.x, this.spurs[0].root.y);

ctx.beginPath();

// Outward side

for(let i=0; i

ctx.lineTo(this.spurs1[i].tip.x, this.spurs1[i].tip.y);

}

// Draw to tip of trunk

ctx.lineTo(this.tip.x, this.tip.y);

// Inward side

for(let i=0; i

ctx.lineTo(this.spurs2[i].tip.x, this.spurs2[i].tip.y);

}

if(seed > .5){

ctx.lineTo(this.spurs[0].root.x, this.spurs[0].root.y);

}

ctx.stroke();

ctx.fill();

}

const Spur = function(root, tip){

this.root = root;

this.tip = tip;

}

Spur.prototype.drawSpur = function(){

ctx.beginPath();

ctx.moveTo(this.root.x, this.root.y);

ctx.lineTo(this.tip.x, this.tip.y);

ctx.stroke();

}

function getPolarPoint(distance, degree, origin){

let radian = degree * PI180;

return {x:distance * mcos(radian) + origin.x, y:distance * msin(radian) + origin.y}

}

function create(){

ctx.clearRect(0,0,canvasSize,canvasSize);

sections = [];

let offset = 0;//mrandom() * 360;

let howMany = mround(mrandom()*5+5);

let t0 = new Trunk(midpt, offset);

t0.generate();

let sharedStructure = t0.clone();

sections.push(t0);

for(let i=1; i

let t = new Trunk(midpt, ((360 / howMany) * i) + offset);

t.buildFromClone(sharedStructure);

sections.push(t);

}

}

function useCanvasAsBackground(){

offscreen.height = offscreen.width = offscreenSize;

offscreenCtx.drawImage(canvas, 0, 0, offscreenSize, offscreenSize);

let url = offscreen.toDataURL('image/png');

paper.style.backgroundImage = 'url(' + url + ')';

}

function draw(){

let seed = mrandom();

for(let i=0; i

sections[i].draw(seed);

}

useCanvasAsBackground();

}

function animate(){

create();

draw();

// setTimeout(() => animate(), 1000);

}

animate();

總結

以上是生活随笔為你收集整理的html桌面雪花,html5 canvas雪花形状在线生成器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。