var IsoBlock = IsoBlock || {}; IsoBlock.makeFigure = function(options) { // extract options var canvasId = options.canvas; var blocks = options.blocks; var shouldSortBlocks = (options.sortBlocks == undefined) ? true : options.sortBlocks; var shouldDrawAxes = options.drawAxis; var shouldDrawPlane = options.drawPlane; var axisLen = options.axisLen; var silhouette = options.silhouette; // set canvas and context. var canvas = document.getElementById(canvasId); var ctx = canvas.getContext('2d'); // extract scale and origin (camera attributes) var scale = (options.scale && options.scale(canvas.width,canvas.height)) || (canvas.height / 8); var origin = (options.origin && options.origin(canvas.width,canvas.height)) || {x: canvas.width/2, y: canvas.height }; // compute appropriate axis length (assuming origin is horizontally centered) if (!axisLen) { // make sure the axis extends to the edge of the canvas axisLen = Math.floor((canvas.width - origin.x) / scale / Math.cos(Math.PI/6)) - 0.5; } // Get horizontal axis' vertical displacement from origin. var hAxisV = origin.y/scale - 1; // create camera and painter. var camera = new IsoBlock.Camera(origin, scale); var painter = new IsoBlock.Painter(camera); // draw the xy grid function drawGrid() { // grid step var step = 1; // grid range var maxx = 15; var maxy = 15; // plot x lines ctx.beginPath(); for (x=-maxx; x<=maxx; x+=step) { painter.moveTo(ctx, {x:x, y:-maxy}); painter.lineTo(ctx, {x:x, y:maxy}); } // plot y lines for (y=-maxy; y<=maxy; y+=step) { painter.moveTo(ctx, {x:-maxx, y:y}); painter.lineTo(ctx, {x:maxx, y:y}); } // draw grid lines ctx.strokeStyle = "#d7d7d7"; ctx.lineWidth = 1; ctx.stroke(); }; // draw the xy axes and range bars for each block. function drawAxes() { var axisColor = "#444"; ctx.lineWidth = 1; ctx.strokeStyle = axisColor; ctx.fillStyle = axisColor; var arrowSize = 0.3; // draw x,y axes (and h axis if silhouette) var xAxisStart = camera.spaceToIso({x:-axisLen, y:0}); var xAxisEnd = camera.spaceToIso({x:axisLen, y:0}); var yAxisStart = camera.spaceToIso({x:0, y:-axisLen}); var yAxisEnd = camera.spaceToIso({x:0, y:axisLen}); var hAxisStart = {h:yAxisEnd.h, v:hAxisV}; var hAxisEnd = {h:xAxisEnd.h, v:hAxisV}; ctx.beginPath(); painter.moveTo(ctx, xAxisStart); painter.lineTo(ctx, xAxisEnd); painter.moveTo(ctx, yAxisStart); painter.lineTo(ctx, yAxisEnd); if (silhouette) { painter.moveTo(ctx, hAxisStart); painter.lineTo(ctx, hAxisEnd); } ctx.stroke(); // draw x-axis arrow ctx.beginPath(); painter.moveTo(ctx, {x:axisLen, y:0}); painter.lineTo(ctx, {x:axisLen-arrowSize, y:-arrowSize}); painter.lineTo(ctx, {x:axisLen-arrowSize, y:arrowSize}); ctx.closePath(); ctx.fill(); // draw y-axis arrow ctx.beginPath(); painter.moveTo(ctx, {y:axisLen, x:0}); painter.lineTo(ctx, {y:axisLen-arrowSize, x:-arrowSize}); painter.lineTo(ctx, {y:axisLen-arrowSize, x:arrowSize}); ctx.closePath(); ctx.fill(); // draw h-axis arrow if (silhouette) { ctx.beginPath(); painter.moveTo(ctx, hAxisStart); painter.lineTo(ctx, {h:hAxisStart.h+arrowSize, v:hAxisV+arrowSize}); painter.lineTo(ctx, {h:hAxisStart.h+arrowSize, v:hAxisV-arrowSize}); ctx.closePath(); ctx.fill(); ctx.beginPath(); painter.moveTo(ctx, hAxisEnd); painter.lineTo(ctx, {h:hAxisEnd.h-arrowSize, v:hAxisV+arrowSize}); painter.lineTo(ctx, {h:hAxisEnd.h-arrowSize, v:hAxisV-arrowSize}); ctx.closePath(); ctx.fill(); } // draw axis labels var p = painter.transform({x:axisLen-1, y:-1}); ctx.font = "italic 1em serif"; ctx.textBaseline='middle'; ctx.textAlign='right'; ctx.fillText("x",p.x,p.y); p = painter.transform({x:-1, y:axisLen-1}); ctx.textAlign='left'; ctx.fillText("y",p.x,p.y); if (silhouette) { p = painter.transform({h:hAxisEnd.h, v:hAxisV-1}); ctx.textAlign='right'; ctx.fillText("h",p.x,p.y); } // draw axis ranges for each block var i,len,bounds,color,rgb,minp,maxp; var s = 0.25; for (i=0, len=blocks.length; i0 && i==len-1) { drawSeparationPlane(blocks[i], blocks[i-1]); } // draw block drawBlock(blocks[i]); } };