scratch-html5/js/Stage.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

2013-10-28 16:00:20 -04:00
// Copyright (C) 2013 Massachusetts Institute of Technology
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 2,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// Scratch HTML5 Player
// Stage.js
// Tim Mickel, July 2011 - March 2012
// Provides the basic logic for the Stage, a special kind of Sprite.
'use strict';
var Stage = function(data) {
// Place the background layer in the very back.
// The pen layer is right above the stage background,
// and all sprites are above that.
this.z = -2;
// Pen layer and canvas cache.
this.penLayerLoaded = false;
this.lineCanvas = document.createElement('canvas');
this.lineCanvas.width = 480;
this.lineCanvas.height = 360;
this.lineCache = this.lineCanvas.getContext('2d');
2014-03-08 02:46:59 -05:00
this.isStage = true;
2013-10-28 16:00:20 -04:00
Sprite.call(this, data);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Stage.prototype = Object.create(Sprite.prototype);
Stage.prototype.constructor = Stage;
2013-11-01 22:44:51 -04:00
2013-10-28 16:00:20 -04:00
Stage.prototype.attachPenLayer = function(scene) {
2013-11-01 22:44:51 -04:00
if (this.penLayerLoaded) return;
2013-10-28 16:00:20 -04:00
this.penLayerLoaded = true;
$(this.lineCanvas).css('position', 'absolute');
$(this.lineCanvas).css('z-index', '-1');
scene.append(this.lineCanvas);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Stage.prototype.isLoaded = function() {
2013-11-01 22:44:51 -04:00
return this.penLayerLoaded && this.costumesLoaded == this.costumes.length && this.soundsLoaded == Object.keys(this.sounds).length;
};
2013-10-28 16:00:20 -04:00
// Pen functions
Stage.prototype.clearPenStrokes = function() {
this.lineCache.clearRect(0, 0, 480, 360);
2013-11-01 22:44:51 -04:00
};
2013-10-28 16:00:20 -04:00
Stage.prototype.stroke = function(from, to, width, color) {
this.lineCache.lineWidth = width;
this.lineCache.lineCap = 'round';
this.lineCache.beginPath();
// Use .5 offsets for canvas rigid pixel drawing
this.lineCache.moveTo(from[0] + 240.5, 180.5 - from[1]);
this.lineCache.lineTo(to[0] + 240.5, 180.5 - to[1]);
this.lineCache.strokeStyle = 'rgb(' + (color >> 16) + ',' + (color >> 8 & 255) + ',' + (color & 255) + ')';
this.lineCache.stroke();
2013-11-01 22:44:51 -04:00
};