canvas绘制竹林效果代码
代码语言:html
所属分类:其他
代码描述:canvas绘制竹林效果代码,点击可重新绘制
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> * { font-family: 'arial black', Helvetica, verdana, monospace, sans-serif; letter-spacing: 0.2rem; margin: 0; padding: 0; color: #FFF; overflow: hidden; } body { position: relative; } canvas#canvas { display: block; background: #000; } span#logo { display: inline-block; width: 1.6rem; height: 1.6rem; border-radius: 50%; background: #CC2F3B; margin-right: 0.4rem; } h1 { position: absolute; top: 1.6rem; left: 1.6rem; font-size: 2.4rem; } h2 { background: rgba(0, 0, 0, 0.7); position: absolute; top: 0; right: 1.6rem; height: 100%; text-align: center; font-size: 1rem; padding: 0.8rem; -webkit-writing-mode: vertical-rl; -ms-writing-mode: tb-rl; writing-mode: vertical-rl; } </style> </head> <body> <!-- partial:index.partial.html --> <canvas id="canvas">Canvas not supported.</canvas> <h1><span id="logo"></span>Pattern</h1> <h2 id="title"></h2> <!-- partial --> <script > (function () { 'use strict'; window.addEventListener('load', function () { var canvas = document.getElementById('canvas'); if (!canvas || !canvas.getContext) { return false; } /******************** Random Number ********************/ function rand(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); } /******************** Change Title ********************/ var title = document.getElementById('title'); title.textContent = 'CHIKURIN / 竹林'; /******************** Var ********************/ var ctx = canvas.getContext('2d'); var X = canvas.width = window.innerWidth; var Y = canvas.height = window.innerHeight; var shapes = []; var style = { black: 'black', white: 'white', lineWidth: 4, }; /******************** Animation ********************/ window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(cb) { setTimeout(cb, 17); }; /******************** Shape ********************/ function Shape(ctx, x, y, w, h, n) { this.ctx = ctx; this.init(x, y, w, h, n); } Shape.prototype.init = function(x, y, w, h, n) { this.x = x; this.y = y; this.w = w; this.h = h; this.n = n; this.d = 3; this.a = rand(-5, 5) * Math.random(); this.rad = this.a * Math.PI / 180; this.random = Math.random(); }; Shape.prototype.draw = function() { var ctx = this.ctx; ctx.save(); ctx.globalAlpha = this.random; ctx.fillStyle = style.white; ctx.translate(this.x, this.y); //ctx.rotate(0.01 * Math.sin(this.rad)); ctx.rotate(this.rad); ctx.translate(-this.x, -this.y); for (var i = 0; i < this.n; i++) { ctx.beginPath(); ctx.moveTo(this.x - this.w / 2, this.y - this.h * i - this.d * i); ctx.quadraticCurveTo(this.x - this.w / 5, this.y - this.h * i - this.d * i - this.h / 2, this.x - this.w / 2, this.y - this.h * i - this.d * i - this.h); ctx.lineTo(this.x + this.........完整代码请登录后点击上方下载按钮下载查看
网友评论0