canvas实现多种树叶铺满屏幕效果代码
代码语言:html
所属分类:其他
代码描述:canvas实现多种树叶铺满屏幕效果代码,点击切换效果。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
height: 100vh;
width: 100vw;
overflow:hidden;
}
canvas {
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
(function () {
//fields
var canvas = document.getElementById("canvas");
var body = document.getElementsByTagName("body")[0];
var positions = []
var ctx = canvas.getContext("2d");
/*Initialize flowers*/
body.addEventListener("click",function(ev){
drawFlowers(false)
})
window.onload = ()=>{drawFlowers(true)}
window.onresize = ()=>{drawFlowers(false)}
/*Flower flunctions*/
function Flower(opt) {
this.r = opt.allowLarge && chance(.05) ? getRandomInt(50,150) : getRandomInt(20,50)
this.x = getRandomInt(0, canvas.width)
this.y = getRandomInt(0, canvas.height)
this.lw = 4;
this.angle = getRandomInt(-30,30)
this.rad = this.angle * (Math.PI / 180) /*FLOWER ROTATION*/
this.opacity = 1;
this.petals = chance(.03) ? 4 : 3;
this.shiftOut = this.petals == 4 ? 0 : 0
this.petalBase = 0;
this.petalSpread1 = getRandomFloat(opt.minSpread, opt.maxSpread)
this.petalLength1 = this.petalSpread1 * getRandomFloat(1.4, 1.8) /*length 1.5 longer than spread*/
this.petalSpread2 = getRandomFloat(this.petalSpread1+.15,this.petalSpread1-.15);
this.petalLength2 = this.petalLength1
this.leafRotation = Math.floor(360/this.petals)/*PETAL ROTATION*/
this.color1 = vary(opt.color1, opt.colorVariance)
}
function drawFlowers(firstTime) {
scaleCanvas()
ctx.clearRect(0, 0, canvas.width, canvas.height)
positions = [];
//drawing options
var opt = {
spaceAround:chance(),
allowLarge:chance(0),
padding:getRandomFloat(.5,3), /*multiplier*/
color1:pick(greens),
stems: chance(.7),
stemLength : getRandomInt(20,(chance(.2)?150:60)), /*multiplier*/
stemWidth:getRandomInt(2,5), /*multiplier*/
colorVariance:getRandomInt(10,20),
minSpread:getRandomFloat(.4,.7),
maxSpread:getRandomFloat(.4,1.2),
fieldEffect:firstTime || chance(.33),
randomY:chance()
}
.........完整代码请登录后点击上方下载按钮下载查看
















网友评论0