var utils = require('./utils.js'); var consts = require('./consts.js'); var lineColor = consts.GRID_LINE_COLOR; var boxBorderColor = consts.BOX_BORDER_COLOR; //Draw a single line in canvas context var drawLine = function(ctx,p1,p2,color){ ctx.beginPath(); ctx.moveTo(p1.x,p1.y); ctx.lineTo(p2.x,p2.y); ctx.lineWidth=1; ctx.strokeStyle= color; ctx.stroke(); ctx.closePath(); }; //Draw game grids var drawGrids = function(el,gridSize,colCount,rowCount,color1,color2){ var ctx = el.getContext('2d'); var width = el.width; var height = el.height; ctx.rect(0, 0, width, height); var grd = ctx.createLinearGradient(0, 0, 0, height); grd.addColorStop(0, color1); grd.addColorStop(1, color2); ctx.fillStyle = grd; ctx.fill(); for (var i = 1; i < colCount; i++) { var x = gridSize*i+0.5; drawLine(ctx,{x:x,y:0},{x:x,y:height},lineColor); }; for (var i = 1; i < rowCount; i++) { var y = gridSize*i+0.5; drawLine(ctx,{x:0,y:y},{x:width,y:y},lineColor); }; }; //Draw box of shape (shape is the composition of boxes) var drawBox = function(ctx,color,x,y,gridSize){ if (y<0){ return; } ctx.beginPath(); ctx.rect(x,y,gridSize,gridSize); ctx.fillStyle = color; ctx.fill(); ctx.strokeStyle= boxBorderColor; ctx.lineWidth=1; ctx.stroke(); ctx.closePath(); } /* Canvas main object, use to draw all games data. */ var tetrisCanvas = { init:function(scene,preview,hold){ this.scene = scene; this.preview = preview; this.hold = hold; this.sceneContext = scene.getContext('2d'); this.previewContext = preview.getContext('2d'); this.holdContext = hold.getContext('2d'); this.gridSize = scene.width / consts.COLUMN_COUNT; this.previewGridSize = preview.width / 4;//consts.PREVIEW_COUNT; this.holdGridSize = preview.width / 4;//consts.PREVIEW_COUNT; this.drawScene(); }, //Clear game canvas clearScene:function(){ this.sceneContext.clearRect(0, 0, this.scene.width, this.scene.height); }, //Clear preview canvas clearPreview:function(){ this.previewContext.clearRect(0,0,this.preview.width,this.preview.height); }, //Clear preview canvas clearHold:function(){ this.holdContext.clearRect(0,0,this.hold.width,this.hold.height); }, //Draw game scene, grids drawScene:function(){ this.clearScene(); drawGrids(this.scene,this.gridSize, consts.COLUMN_COUNT,consts.ROW_COUNT, consts.SCENE_BG_START,consts.SCENE_BG_END); }, //Draw game data drawMatrix:function(matrix){ for(var i = 0;i { if(shape != undefined) { var matrix = shape.matrix(); var gsize = this.previewGridSize; var startX = (this.preview.width - gsize*shape.getColumnCount()) / 2; var startY = ((this.preview.height - gsize*shape.getRowCount()) / 2 / 4)*(index*2+1); for(var i = 0;i { if(shape != undefined) { var matrix = shape.matrix(); var gsize = this.holdGridSize; var startX = (this.hold.width - gsize*shape.getColumnCount()) / 2; var startY = ((this.hold.height - gsize*shape.getRowCount()) / 2 / 4)*(index*2+1); for(var i = 0;i