Last active
August 29, 2015 14:10
-
-
Save Nullpo/484404f053ddefcb7734 to your computer and use it in GitHub Desktop.
Jugando con Canvas.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE> | |
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <canvas id="canvas" width="800" height="600" style="background-color:black"></canvas> | |
| <script type="text/javascript" src="micanvas.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * @param config Example of config: | |
| * { | |
| * id: "aCanvas", | |
| * maxX : 100, | |
| * maxY : 200, | |
| * lineColor : 'red' | |
| * } | |
| * @constructor | |
| */ | |
| var ElGrafico = function(config) { | |
| "use strict"; | |
| this.container = document.getElementById(config.id); | |
| this.canvas = document.createElement("canvas"); | |
| this.canvas.style.height = "100%"; | |
| this.canvas.style.width = "100%"; | |
| this.container.appendChild(this.canvas); | |
| this.canvas.width = this.canvas.offsetWidth; | |
| this.canvas.height = this.canvas.offsetHeight; | |
| this.context = this.canvas.getContext('2d'); | |
| this.context.lineWidth = 2; | |
| this.virtualSize = { | |
| width : config.maxX, //Esto tiene que parametrizarse | |
| height: config.maxY | |
| }; | |
| this.virtualLastPoint = { | |
| x: 0, | |
| y: Math.floor(this.virtualSize.height / 2) | |
| }; | |
| this.realLastPoint = { | |
| x: 0, | |
| y: this.realSize / 2 | |
| }; | |
| this.realSize = { | |
| width: this.canvas.width, | |
| height: this.canvas.height | |
| }; | |
| this.ratio = { | |
| tickX: this.realSize.width / this.virtualSize.width, | |
| tickY: this.realSize.height / this.virtualSize.height | |
| }; | |
| this.context.strokeStyle = config.lineColor; | |
| }; | |
| // Pasa de coordenadas virtuales a coordenadas reales. | |
| ElGrafico.prototype.calculatePositon = function(virtualX, virtualY) { | |
| "use strict"; | |
| return { | |
| x: Math.floor(virtualX * this.ratio.tickX), | |
| y: Math.floor(virtualY * this.ratio.tickY) | |
| }; | |
| }; | |
| ElGrafico.prototype.draw = function(newValueInCartesian) { | |
| "use strict"; | |
| // Cartesian to screen! | |
| var context = this.context, | |
| newValue = this.virtualSize.height - newValueInCartesian, | |
| actualRealPos = this.calculatePositon(this.virtualLastPoint.x, newValue); | |
| context.beginPath(); | |
| context.moveTo(this.realLastPoint.x, this.realLastPoint.y); | |
| context.lineTo(actualRealPos.x, actualRealPos.y); | |
| context.stroke(); | |
| context.closePath(); | |
| this.virtualLastPoint.y = newValue; | |
| this.realLastPoint = actualRealPos; | |
| this.virtualLastPoint.x++; | |
| if (this.virtualLastPoint.x > this.virtualSize.width - 1) { | |
| this.virtualLastPoint.x = 0; | |
| this.realLastPoint.x = 0; | |
| } | |
| this.cleanNextColumn(); | |
| }; | |
| ElGrafico.prototype.cleanNextColumn = function() { | |
| this.context.clearRect(this.realLastPoint.x, 0, 4, this.realSize.height); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment