Created
November 13, 2011 16:30
-
-
Save mattneary/1362294 to your computer and use it in GitHub Desktop.
Plot functions in JavaScript canvas. Arguments: function, canvas element, scaleWindow object
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
function plotF( f, elem, scaleWindow) { | |
var scaleWindow = scaleWindow || { | |
xMin: -8, | |
xMax: 8, | |
yMin: -8, | |
yMax: 8 | |
}; | |
scaleWindow.yMax = [-scaleWindow.yMin, (scaleWindow.yMin = -scaleWindow.yMax)][0]; //Swap Max/Min to make more intuitive | |
var pdy = elem.height/(scaleWindow.yMax-scaleWindow.yMin), | |
pdx = elem.width/(scaleWindow.xMax-scaleWindow.xMin); | |
axies = { | |
y: (elem.width*(0-scaleWindow.xMin)/(scaleWindow.xMax-scaleWindow.xMin)), | |
x: (elem.height*(0-scaleWindow.yMin)/(scaleWindow.yMax-scaleWindow.yMin)) | |
}; | |
// Always check for properties and methods, to make sure your code doesn't break | |
// in other browsers. | |
if (elem && elem.getContext) { | |
// Get the 2d context. | |
// Remember: you can only initialize one context per element. | |
var context = elem.getContext('2d'); | |
if (context) { | |
context.beginPath(); | |
//Axis Style | |
context.strokeStyle = '#000'; | |
context.lineWidth = 1; | |
//y-axis | |
context.moveTo( axies.y, 0 ); | |
context.lineTo( axies.y, elem.height ); | |
//x-axis | |
context.moveTo( 0, axies.x ); | |
context.lineTo( elem.width, axies.x ); | |
context.stroke(); | |
context.closePath(); | |
context.beginPath(); | |
//Plot style | |
context.strokeStyle = '#f00'; | |
context.fillStyle = '#00f'; | |
context.lineWidth = 3; | |
var sum = 0, | |
sumN = 0; | |
for( var i = axies.y/elem.width * -50; i < (elem.width-axies.y)/elem.width * 50; i++ ) { | |
var fraction = function(n) { return (n - axies.y/elem.width * -50)/50; }, | |
xMin = scaleWindow.xMin, | |
xRange = scaleWindow.xMax - scaleWindow.xMin, | |
yRange = scaleWindow.yMax - scaleWindow.yMin; | |
context.moveTo(fraction(i)*elem.width, axies.x-f(xMin+fraction(i)*xRange)/yRange*elem.height); // give the (x,y) coordinates | |
context.lineTo(fraction(i+1)*elem.width, axies.x-f(xMin+fraction(i+1)*xRange)/yRange*elem.height); | |
} | |
context.fill(); | |
context.stroke(); | |
context.closePath(); | |
} | |
} | |
} |
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
function plotF(a,b,c){var c=c||{xMin:-8,xMax:8,yMin:-8,yMax:8};c.yMax=[-c.yMin,c.yMin=-c.yMax][0];var d=b.height/(c.yMax-c.yMin),e=b.width/(c.xMax-c.xMin);axies={y:b.width*(0-c.xMin)/(c.xMax-c.xMin),x:b.height*(0-c.yMin)/(c.yMax-c.yMin)};if(b&&b.getContext){var f=b.getContext("2d");if(f){f.beginPath();f.strokeStyle="#000";f.lineWidth=1;f.moveTo(axies.y,0);f.lineTo(axies.y,b.height);f.moveTo(0,axies.x);f.lineTo(b.width,axies.x);f.stroke();f.closePath();f.beginPath();f.strokeStyle="#f00";f.fillStyle="#00f";f.lineWidth=3;var g=0,h=0;for(var i=axies.y/b.width*-50;i<(b.width-axies.y)/b.width*50;i++){var j=function(a){return(a-axies.y/b.width*-50)/50},k=c.xMin,l=c.xMax-c.xMin,m=c.yMax-c.yMin;f.moveTo(j(i)*b.width,axies.x-a(k+j(i)*l)/m*b.height);f.lineTo(j(i+1)*b.width,axies.x-a(k+j(i+1)*l)/m*b.height)}f.fill();f.stroke();f.closePath()}}} | |
//843 chars! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment