Heart Surface wellflat Follow 2010-07-27 23:53:28 License: MIT License Fork1 Fav11 View1881 Play Stop Reload Fullscreen Smart Phone Fork tree Readme JavaScript 88 lines HTML 21 lines CSS 4 lines Heart Surface var Heart = function(canvas, n) { this.initialize.apply(this, arguments); }; Heart.prototype = { initialize : function(canvas, n) { if(canvas == undefined) return ; this.context = canvas.getContext('2d'); this.WIDTH = canvas.width; this.HEIGHT = canvas.height; this.SCALE = 25; this.FOV = 250; this.points = []; this.numPoints = 0; this.isPlay = false; this.setStyle(this.context.createLinearGradient(0, 0, 0, 300)); this.setPoints(n); }, setStyle : function(grad) { grad.addColorStop(0, 'rgb(255, 255, 255)'); grad.addColorStop(0.7, 'rgb(196, 196, 196)'); grad.addColorStop(0.8, 'rgb(160, 160, 160)'); grad.addColorStop(0.9, 'rgb(128, 128, 128)'); grad.addColorStop(1, 'rgb(96, 96, 96)'); this.context.fillStyle = grad; this.context.strokeStyle = 'rgb(255, 0, 160)'; }, setPoints : function(num) { var r = 0.0; var theta = 0.0; var z = 0.0; var n = 0; for(i=-Math.PI*num; i<=Math.PI*num; i++) { for(j=-num; j<=num; j++) { theta = i/num; z = j/num; r = 4*Math.sqrt(1 - z*z)*Math.pow(Math.sin(Math.abs(theta)), Math.abs(theta)); point = [(this.SCALE*r*Math.sin(theta)), (this.SCALE*r*Math.cos(theta)), (this.SCALE*z)]; this.points.push(point); n++; } } this.numPoints = n; }, play : function(t, angle) { var self = this; if(!this.isPlay) { this.timerID = setInterval(function(){self.render(angle);}, t); this.isPlay = true; } }, stop : function() { if(this.isPlay) { clearInterval(this.timerID); this.isPlay = false; } }, render : function(angle) { this.context.fillRect(0, 0, this.WIDTH, this.HEIGHT); for(i=0; i<this.numPoints; i++) { this.rotateY(this.points[i], angle); this.draw(this.points[i]); } }, draw : function(point3d) { var scale = this.FOV/(this.FOV + point3d[2]); var x = (point3d[0]*scale) + this.WIDTH/2; var y = (point3d[1]*scale) + this.HEIGHT/2; this.context.lineWidth= scale*2; this.context.beginPath(); this.context.moveTo(x, y); this.context.lineTo(x + scale, y); this.context.stroke(); }, rotateY : function(point3d, angle) { var x = point3d[0]; var z = point3d[2]; var c = Math.cos(angle); var s = Math.sin(angle); var tempZ = z; var tempX = x; x = tempX*c + tempZ*s; z = tempZ*c - tempX*s; point3d[0] = x; point3d[2] = z; } }; <section> <h2>Heart Surface</h2> <canvas id="Canvas" width="400" height="300"></canvas> <input id="PlayButton" type="button" value=" play " > <input id="StopButton" type="button" value=" stop " > </section> <script> window.addEventListener('load', function(e) { var heart = new Heart(document.getElementById('Canvas'), 8); var playBtn = document.getElementById('PlayButton'); var stopBtn = document.getElementById('StopButton'); heart.play(50, 0.03); playBtn.addEventListener('click', function(e) { heart.play(50, 0.03); }, false); stopBtn.addEventListener('click', function(e) { heart.stop(); }, false); }, false); </script> Heart Surface body{ font-size:12px; color:#ccc; background-color:#000; text-align:center; margin:20px; } canvas{ border:1px solid #ff0099; margin: 10px auto; display: block; } input[type="button"]{ font-family:Verdana, sans-serif; color:#fff; background-color:#666; border:none; padding:2px 8px; } input[type="button"]:hover{ background-color: #333; } var Heart = function(canvas, n) { this.initialize.apply(this, arguments); }; Heart.prototype = { initialize : function(canvas, n) { if(canvas == undefined) return ; this.context = canvas.getContext('2d'); this.WIDTH = canvas.width; this.HEIGHT = canvas.height; this.SCALE = 25; this.FOV = 250; this.points = []; this.numPoints = 0; this.isPlay = false; this.setStyle(this.context.createLinearGradient(0, 0, 0, 300)); this.setPoints(n); }, setStyle : function(grad) { grad.addColorStop(0, 'rgb(255, 255, 255)'); grad.addColorStop(0.7, 'rgb(196, 196, 196)'); grad.addColorStop(0.8, 'rgb(160, 160, 160)'); grad.addColorStop(0.9, 'rgb(128, 128, 128)'); grad.addColorStop(1, 'rgb(96, 96, 96)'); this.context.fillStyle = grad; this.context.strokeStyle = 'rgb(255, 0, 160)'; }, setPoints : function(num) { var r = 0.0; var theta = 0.0; var z = 0.0; var n = 0; for(i=-Math.PI*num; i<=Math.PI*num; i++) { for(j=-num; j<=num; j++) { theta = i/num; z = j/num; r = 4*Math.sqrt(1 - z*z)*Math.pow(Math.sin(Math.abs(theta)), Math.abs(theta)); point = [(this.SCALE*r*Math.sin(theta)), (this.SCALE*r*Math.cos(theta)), (this.SCALE*z)]; this.points.push(point); n++; } } this.numPoints = n; }, play : function(t, angle) { var self = this; if(!this.isPlay) { this.timerID = setInterval(function(){self.render(angle);}, t); this.isPlay = true; } }, stop : function() { if(this.isPlay) { clearInterval(this.timerID); this.isPlay = false; } }, render : function(angle) { this.context.fillRect(0, 0, this.WIDTH, this.HEIGHT); for(i=0; i<this.numPoints; i++) { this.rotateY(this.points[i], angle); this.draw(this.points[i]); } }, draw : function(point3d) { var scale = this.FOV/(this.FOV + point3d[2]); var x = (point3d[0]*scale) + this.WIDTH/2; var y = (point3d[1]*scale) + this.HEIGHT/2; this.context.lineWidth= scale*2; this.context.beginPath(); this.context.moveTo(x, y); this.context.lineTo(x + scale, y); this.context.stroke(); }, rotateY : function(point3d, angle) { var x = point3d[0]; var z = point3d[2]; var c = Math.cos(angle); var s = Math.sin(angle); var tempZ = z; var tempX = x; x = tempX*c + tempZ*s; z = tempZ*c - tempX*s; point3d[0] = x; point3d[2] = z; } }; <section> <h2>Heart Surface</h2> <canvas id="Canvas" width="400" height="300"></canvas> <input id="PlayButton" type="button" value=" play " > <input id="StopButton" type="button" value=" stop " > </section> <script> window.addEventListener('load', function(e) { var heart = new Heart(document.getElementById('Canvas'), 8); var playBtn = document.getElementById('PlayButton'); var stopBtn = document.getElementById('StopButton'); heart.play(50, 0.03); playBtn.addEventListener('click', function(e) { heart.play(50, 0.03); }, false); stopBtn.addEventListener('click', function(e) { heart.stop(); }, false); }, false); </script> body{ font-size:12px; color:#ccc; background-color:#000; text-align:center; margin:20px; } canvas{ border:1px solid #ff0099; margin: 10px auto; display: block; } input[type="button"]{ font-family:Verdana, sans-serif; color:#fff; background-color:#666; border:none; padding:2px 8px; } input[type="button"]:hover{ background-color: #333; } use an iframe compat browser, deer Tweet QR code Embed Design view Code view <script type="text/javascript" src="http://jsdo.it/blogparts/qRfA/js?view=design"></script><p class="ttlBpJsdoit" style="width: 465px; margin: 0; text-align: right; font-size: 11px;"><a href="http://jsdo.it/wellflat/qRfA" title="Heart Surface">Heart Surface - jsdo.it - share JavaScript, HTML5 and CSS</a></p> zip tags 3D Canvas Tweet twitter Tags 3D Canvas Favorite by cuddlephish h6k nanlow m1m0r1 siouxcitizen.. coppieee paq matsu4512 clockmaker reosablo: 3D nodamushi: 綺麗ですね。 Forked sort new page view favorite forked forked: Heart Surface davidebrr 00 256views 89/21/4 3D Canvas forked from Heart Surface Jorge.De.La... 00 358views 89/21/4