web前端|HTML5 Canvas可拖动的弹性大树摇摆动画

HTML5 Canvas可拖动的弹性大树摇摆动画DEMO演示 - 锐客网



css部分style.css

body { background: #e4e6e8; background: -moz-linear-gradient(top,#ffffff 0%, #e4e8ee 20%, #e4e8ee 80%, #ffffff 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(20%,#e4e8ee), color-stop(80%,#e4e8ee), color-stop(100%,#ffffff)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top,#ffffff 0%,#e4e8ee 20%,#e4e8ee 80%,#ffffff 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top,#ffffff 0%,#e4e8ee 20%,#e4e8ee 80%,#ffffff 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top,#ffffff 0%,#e4e8ee 20%,#e4e8ee 80%,#ffffff 100%); /* IE10+ */ background: linear-gradient(to bottom,#ffffff 0%,#e4e8ee 20%,#e4e8ee 80%,#ffffff 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ffffff',GradientType=0 ); /* IE6-9 */ color: #969ba3; font-family: "Libre Baskerville", sans-serif; font-weight: 400; line-height: 1.2em; }a { color: #26ae90; }a:hover { color: #0b6b6a; }b { color: #696e76; }ul { list-style: square; }li { margin: 4px 0px; }#header { width: 800px; margin: 0px auto; font-size: 10pt; }h1 { display: block; font-weight: 700; margin: 50px 0px 0px 0px; padding-bottom: 3px; border-bottom: 1px solid rgba(0,0,0,0.05); text-shadow: 1px 1px 0px #fff; }h1:after { content: ""; display: block; padding-bottom: 20px; border-bottom: 1px solid rgba(0,0,0,0.05); }h1 a { color: #969ba3; text-decoration: none; }h1 a:hover { color: #585a5d; text-decoration: none; }h1 em { color: #444; font-style: normal; }h4 { color: #444; font-weight: 700; font-size: 12pt; margin: 20px 0px 0px 0px; }canvas { display: block; margin: 5px auto; background: #fff; box-shadow: 2px 2px 8px 0px rgba(0,0,0,0.1); clear: both; /* disable selection of canvas */ -moz-user-select: none; -webkit-user-select: none; -o-user-select:none; -ms-user-select:none; -khtml-user-select:none; user-select: none; }#footer { line-height: 1.5em; font-size: 8pt; border-top: 1px solid rgba(0,0,0,0.05); padding-top: 3px; width: 800px; margin: 0px auto 100px auto; }#footer:before { content: ""; display: block; padding-top: 20px; border-top: 1px solid rgba(0,0,0,0.05); }#bsa { display: inline-block; float: right; width: 150px; padding-top: 30px; font-family: sans-serif; }body .one .bsa_it_ad { background: transparent; border: none; font-family: inherit; padding: 0 15px 0 10px; margin: 0; text-align: left; } body .one .bsa_it_ad:hover img { -moz-box-shadow: 0 0 3px #000; -webkit-box-shadow: 0 0 3px #000; box-shadow: 0 0 3px #000; } body .one .bsa_it_ad .bsa_it_i { display: block; padding: 0; float: none; margin: 0 0 5px; } body .one .bsa_it_ad .bsa_it_i img { padding: 0; border: none; } body .one .bsa_it_ad .bsa_it_t { padding: 6px 0; } body .one .bsa_it_ad .bsa_it_d { padding: 0; font-size: 10px; color: #333; } body .one .bsa_it_p { display: none; } body #bsap_aplink, body #bsap_aplink:hover { display: block; font-size: 9px; margin: 12px 15px 0; text-align: right; }


js部分verlet.js
; (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require; if(!s&&o)return o(n,!0); if(r)return r(n,!0); throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}}; e[n][0].call(u.exports,function(t){var r=e[n][1][t]; return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require; for(var s=0; s= Math.PI) diff -= 2*Math.PI; diff *= stepCoef*this.stiffness; this.a.pos = this.a.pos.rotate(this.b.pos, diff); this.c.pos = this.c.pos.rotate(this.b.pos, -diff); this.b.pos = this.b.pos.rotate(this.a.pos, diff); this.b.pos = this.b.pos.rotate(this.c.pos, -diff); }AngleConstraint.prototype.draw = function(ctx) { ctx.beginPath(); ctx.moveTo(this.a.pos.x, this.a.pos.y); ctx.lineTo(this.b.pos.x, this.b.pos.y); ctx.lineTo(this.c.pos.x, this.c.pos.y); var tmp = ctx.lineWidth; ctx.lineWidth = 5; ctx.strokeStyle = "rgba(255,255,0,0.2)"; ctx.stroke(); ctx.lineWidth = tmp; }},{}],5:[function(require,module,exports){/* Copyright 2013 Sub Protocol and other contributors http://subprotocol.com/Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */// A simple 2-dimensional vector implementationmodule.exports = Vec2function Vec2(x, y) { this.x = x || 0; this.y = y || 0; }Vec2.prototype.add = function(v) { return new Vec2(this.x + v.x, this.y + v.y); }Vec2.prototype.sub = function(v) { return new Vec2(this.x - v.x, this.y - v.y); }Vec2.prototype.mul = function(v) { return new Vec2(this.x * v.x, this.y * v.y); }Vec2.prototype.div = function(v) { return new Vec2(this.x / v.x, this.y / v.y); }Vec2.prototype.scale = function(coef) { return new Vec2(this.x*coef, this.y*coef); }Vec2.prototype.mutableSet = function(v) { this.x = v.x; this.y = v.y; return this; }Vec2.prototype.mutableAdd = function(v) { this.x += v.x; this.y += v.y; return this; }Vec2.prototype.mutableSub = function(v) { this.x -= v.x; this.y -= v.y; return this; }Vec2.prototype.mutableMul = function(v) { this.x *= v.x; this.y *= v.y; return this; }Vec2.prototype.mutableDiv = function(v) { this.x /= v.x; this.y /= v.y; return this; }Vec2.prototype.mutableScale = function(coef) { this.x *= coef; this.y *= coef; return this; }Vec2.prototype.equals = function(v) { return this.x == v.x && this.y == v.y; }Vec2.prototype.epsilonEquals = function(v, epsilon) { return Math.abs(this.x - v.x) <= epsilon && Math.abs(this.y - v.y) <= epsilon; }Vec2.prototype.length = function(v) { return Math.sqrt(this.x*this.x + this.y*this.y); }Vec2.prototype.length2 = function(v) { return this.x*this.x + this.y*this.y; }Vec2.prototype.dist = function(v) { return Math.sqrt(this.dist2(v)); }Vec2.prototype.dist2 = function(v) { var x = v.x - this.x; var y = v.y - this.y; return x*x + y*y; }Vec2.prototype.normal = function() { var m = Math.sqrt(this.x*this.x + this.y*this.y); return new Vec2(this.x/m, this.y/m); }Vec2.prototype.dot = function(v) { return this.x*v.x + this.y*v.y; }Vec2.prototype.angle = function(v) { return Math.atan2(this.x*v.y-this.y*v.x,this.x*v.x+this.y*v.y); }Vec2.prototype.angle2 = function(vLeft, vRight) { return vLeft.sub(this).angle(vRight.sub(this)); }Vec2.prototype.rotate = function(origin, theta) { var x = this.x - origin.x; var y = this.y - origin.y; return new Vec2(x*Math.cos(theta) - y*Math.sin(theta) + origin.x, x*Math.sin(theta) + y*Math.cos(theta) + origin.y); }Vec2.prototype.toString = function() { return "(" + this.x + ", " + this.y + ")"; }function test_Vec2() { var assert = function(label, expression) { console.log("Vec2(" + label + "): " + (expression == true ? "PASS" : "FAIL")); if (expression != true) throw "assertion failed"; }; assert("equality", (new Vec2(5,3).equals(new Vec2(5,3)))); assert("epsilon equality", (new Vec2(1,2).epsilonEquals(new Vec2(1.01,2.02), 0.03))); assert("epsilon non-equality", !(new Vec2(1,2).epsilonEquals(new Vec2(1.01,2.02), 0.01))); assert("addition", (new Vec2(1,1)).add(new Vec2(2, 3)).equals(new Vec2(3, 4))); assert("subtraction", (new Vec2(4,3)).sub(new Vec2(2, 1)).equals(new Vec2(2, 2))); assert("multiply", (new Vec2(2,4)).mul(new Vec2(2, 1)).equals(new Vec2(4, 4))); assert("divide", (new Vec2(4,2)).div(new Vec2(2, 2)).equals(new Vec2(2, 1))); assert("scale", (new Vec2(4,3)).scale(2).equals(new Vec2(8, 6))); assert("mutable set", (new Vec2(1,1)).mutableSet(new Vec2(2, 3)).equals(new Vec2(2, 3))); assert("mutable addition", (new Vec2(1,1)).mutableAdd(new Vec2(2, 3)).equals(new Vec2(3, 4))); assert("mutable subtraction", (new Vec2(4,3)).mutableSub(new Vec2(2, 1)).equals(new Vec2(2, 2))); assert("mutable multiply", (new Vec2(2,4)).mutableMul(new Vec2(2, 1)).equals(new Vec2(4, 4))); assert("mutable divide", (new Vec2(4,2)).mutableDiv(new Vec2(2, 2)).equals(new Vec2(2, 1))); assert("mutable scale", (new Vec2(4,3)).mutableScale(2).equals(new Vec2(8, 6))); assert("length", Math.abs((new Vec2(4,4)).length() - 5.65685) <= 0.00001); assert("length2", (new Vec2(2,4)).length2() == 20); assert("dist", Math.abs((new Vec2(2,4)).dist(new Vec2(3,5)) - 1.4142135) <= 0.000001); assert("dist2", (new Vec2(2,4)).dist2(new Vec2(3,5)) == 2); var normal = (new Vec2(2,4)).normal() assert("normal", Math.abs(normal.length() - 1.0) <= 0.00001 && normal.epsilonEquals(new Vec2(0.4472, 0.89443), 0.0001)); assert("dot", (new Vec2(2,3)).dot(new Vec2(4,1)) == 11); assert("angle", (new Vec2(0,-1)).angle(new Vec2(1,0))*(180/Math.PI) == 90); assert("angle2", (new Vec2(1,1)).angle2(new Vec2(1,0), new Vec2(2,1))*(180/Math.PI) == 90); assert("rotate", (new Vec2(2,0)).rotate(new Vec2(1,0), Math.PI/2).equals(new Vec2(1,1))); assert("toString", (new Vec2(2,4)) == "(2, 4)"); }},{}],4:[function(require,module,exports){var VerletJS = require('./verlet') var Particle = VerletJS.Particle var constraints = require('./constraint') var DistanceConstraint = constraints.DistanceConstraintVerletJS.prototype.point = function(pos) { var composite = new this.Composite(); composite.particles.push(new Particle(pos)); this.composites.push(composite); return composite; }VerletJS.prototype.lineSegments = function(vertices, stiffness) { var i; var composite = new this.Composite(); for (i in vertices) { composite.particles.push(new Particle(vertices[i])); if (i > 0) composite.constraints.push(new DistanceConstraint(composite.particles[i], composite.particles[i-1], stiffness)); } this.composites.push(composite); return composite; }VerletJS.prototype.cloth = function(origin, width, height, segments, pinMod, stiffness) { var composite = new this.Composite(); var xStride = width/segments; var yStride = height/segments; var x,y; for (y=0; y 0) composite.constraints.push(new DistanceConstraint(composite.particles[y*segments+x], composite.particles[y*segments+x-1], stiffness)); if (y > 0) composite.constraints.push(new DistanceConstraint(composite.particles[y*segments+x], composite.particles[(y-1)*segments+x], stiffness)); } } for (x=0; x this.height-1) particle.pos.y = this.height-1; if (particle.pos.x < 0) particle.pos.x = 0; if (particle.pos.x > this.width-1) particle.pos.x = this.width-1; } var _this = this; // prevent context menu this.canvas.oncontextmenu = function(e) { e.preventDefault(); }; this.canvas.onmousedown = function(e) { _this.mouseDown = true; var nearest = _this.nearestEntity(); if (nearest) { _this.draggedEntity = nearest; } }; this.canvas.onmouseup = function(e) { _this.mouseDown = false; _this.draggedEntity = null; }; this.canvas.onmousemove = function(e) { var rect = _this.canvas.getBoundingClientRect(); _this.mouse.x = e.clientX - rect.left; _this.mouse.y = e.clientY - rect.top; }; // simulation params this.gravity = new Vec2(0,0.2); this.friction = 0.99; this.groundFriction = 0.8; // holds composite entities this.composites = []; }VerletJS.prototype.Composite = Compositefunction Composite() { this.particles = []; this.constraints = []; this.drawParticles = null; this.drawConstraints = null; }Composite.prototype.pin = function(index, pos) { pos = pos || this.particles[index].pos; var pc = new PinConstraint(this.particles[index], pos); this.constraints.push(pc); return pc; }VerletJS.prototype.frame = function(step) { var i, j, c; for (c in this.composites) { for (i in this.composites[c].particles) { var particles = this.composites[c].particles; // calculate velocity var velocity = particles[i].pos.sub(particles[i].lastPos).scale(this.friction); // ground friction if (particles[i].pos.y >= this.height-1 && velocity.length2() > 0.000001) { var m = velocity.length(); velocity.x /= m; velocity.y /= m; velocity.mutableScale(m*this.groundFriction); }// save last good state particles[i].lastPos.mutableSet(particles[i].pos); // gravity particles[i].pos.mutableAdd(this.gravity); // inertia particles[i].pos.mutableAdd(velocity); } } // handle dragging of entities if (this.draggedEntity) this.draggedEntity.pos.mutableSet(this.mouse); // relax var stepCoef = 1/step; for (c in this.composites) { var constraints = this.composites[c].constraints; for (i=0; i



【web前端|HTML5 Canvas可拖动的弹性大树摇摆动画】

    推荐阅读