$(document).ready(function(){ var canvas = $("#laser")[0]; var width = 0; var height = 0; init(); function init() { var win = $(document); var ctx = canvas.getContext("2d"); resize(canvas, win.width(), win.height()); var lasers = []; var t = 30; var mx = 360 / (t - 2); for(var d = 0; d < t; d++) { lasers.push(new Laser(width / 2, height / 2, 3, 100, mx * d, (~0xffffff / d) & 0xffffff)); } setInterval(function(){ ctx.beginPath(); ctx.rect(0,0, width, height); ctx.fill(); ctx.closePath(); for(var l = 0; l < lasers.length; l++) { var laser = lasers[l]; var xLaser = laser.CordXLaser, yLaser = laser.CordYLaser, wLaser = laser.WidthLaser, hLaser = laser.HeightLaser, cLaser = laser.ColorLaser.toString(16), rLaser = laser.RotationLaser; /// Draw Laser var lastCord = {x: xLaser, y: yLaser, r: rLaser}; for(var t = 0; t < 2; t++) { var rotate = lastCord.r * (t % 2 ? 1 : -1); ctx.save(); ctx.translate(lastCord.x, lastCord.y); ctx.rotate(rotate * Math.PI / 180); ctx.beginPath(); ctx.shadowBlur = 20; ctx.shadowColor = "#" + cLaser; ctx.rect(-wLaser / 2, 0, wLaser, hLaser); ctx.fillStyle = "#" + cLaser; ctx.closePath(); ctx.fill(); ctx.restore(); lastCord.y += Math.cos(rotate * Math.PI / 180) * hLaser; lastCord.x -= Math.sin(rotate * Math.PI / 180) * hLaser; } ctx.save(); ctx.beginPath(); ctx.arc(xLaser, yLaser, 10, 0, 2 * Math.PI); ctx.closePath(); ctx.fillStyle = "#FFC243"; ctx.fill(); ctx.restore(); laser.Update(); } }, 100 / 6); } $(window).resize(function(){ var win = $(window); resize(canvas, win.width(), win.height()); }); function resize(e, w, h) { width = e.width = w; height = e.height = h; } function Laser(cordX, cordY, width, height, rotation, color) { this.CordXLaser = cordX; this.CordYLaser = cordY; this.SetCords = function(x, y) { this.CordXMirror = x; this.CordYMirror = y; }; this.WidthLaser = width; this.HeightLaser = height; this.SetSizes = function(w, h) { this.WidthMirror = w; this.HeightMirror = h; }; this.LifeLaser = 0; this.RotationLaser = rotation; this.SetRotation = function(r) { this.RotationLaser = r; }; this.ColorLaser = color; this.SetColor = function(c) { this.ColorLaser = c; }; this.Update = function() { this.LifeLaser++; this.RotationLaser += 1; }; } function Mirror(cordX, cordY, width, height, rotation) { this.CordXMirror = cordX; this.CordYMirror = cordY; this.SetCords(x, y) { this.CordXMirror = x; this.CordYMirror = y; }; this.WidthMirror = width; this.HeightMirror = height; this.SetSizes = function(w, h) { this.WidthMirror = w; this.HeightMirror = h; }; this.RotationMirror = rotation; this.SetRotation = function(r) { this.RotationMirror = r; }; this.Update = function() { }; } });