pull/3/merge
yuan 2014-06-02 00:07:00 -07:00
rodzic 8971d29adc
commit e7f9ff5dcb
2 zmienionych plików z 98 dodań i 91 usunięć

Wyświetl plik

@ -918,8 +918,8 @@ SyntaxElementMorph.prototype.labelPart = function (spec) {
negative : ['negative'], negative : ['negative'],
comic: ['comic'], comic: ['comic'],
duplicate: ['duplicate'], duplicate: ['duplicate'],
confetti: ['confetti'], confetti: ['confetti']
}, },
true true
); );
part.setContents(['ghost']); part.setContents(['ghost']);

Wyświetl plik

@ -1292,7 +1292,7 @@ SpriteMorph.prototype.init = function (globals) {
this.idx = 0; // not to be serialized (!) - used for de-serialization this.idx = 0; // not to be serialized (!) - used for de-serialization
this.wasWarped = false; // not to be serialized, used for fast-tracking this.wasWarped = false; // not to be serialized, used for fast-tracking
this.graphicsValues = { 'negative': 0, //dictionary of all the orignal values this.graphicsValues = { 'negative': 0,
'fisheye': 0, 'fisheye': 0,
'whirl': 0, 'whirl': 0,
'pixelate': 0, 'pixelate': 0,
@ -2847,13 +2847,14 @@ SpriteMorph.prototype.changeScale = function (delta) {
this.setScale(this.getScale() + (+delta || 0)); this.setScale(this.getScale() + (+delta || 0));
}; };
// Spritemorph graphics effects // Spritemorph graphic effects
SpriteMorph.prototype.graphicsChanged = function () { SpriteMorph.prototype.graphicsChanged = function () {
var myself = this; var myself = this;
return Object.keys(this.graphicsValues).some( return Object.keys(this.graphicsValues).some(
function(any) { function (any) {
return myself.graphicsValues[any] < 0 || myself.graphicsValues[any] > 0; return myself.graphicsValues[any] < 0 ||
myself.graphicsValues[any] > 0;
} }
); );
}; };
@ -2861,103 +2862,106 @@ SpriteMorph.prototype.graphicsChanged = function () {
SpriteMorph.prototype.applyGraphicsEffects = function (canvas) { SpriteMorph.prototype.applyGraphicsEffects = function (canvas) {
// For every effect: apply transform of that effect(canvas, stored value) // For every effect: apply transform of that effect(canvas, stored value)
// The future: write more effects here // The future: write more effects here
var ctx, imagedata, pixels, newimagedata;
function transform_negative(p, value) { function transform_negative(p, value) {
var i, rcom, gcom, bcom;
if (value !== 0) { if (value !== 0) {
for (i = 0; i < p.length; i = i + 4) { for (i = 0; i < p.length; i += 4) {
var rcom = 255 - p[i + 0] rcom = 255 - p[i];
var gcom = 255 - p[i + 1] gcom = 255 - p[i + 1];
var bcom = 255 - p[i + 2] bcom = 255 - p[i + 2];
if (p[i + 0] < rcom) { //check if current number less than the complement. if so, then if (p[i] < rcom) { //compare to the complement
p[i + 0] = p[i + 0] + value p[i] += value;
} else if (p[i + 0] > rcom) { } else if (p[i] > rcom) {
p[i + 0] = p[i + 0] - value //or else decrease towards it p[i] -= value;
} }
if (p[i + 1] < gcom) { if (p[i + 1] < gcom) {
p[i + 1] = p[i + 1] + value p[i + 1] += value;
} else if (p[i + 1] > gcom) { } else if (p[i + 1] > gcom) {
p[i + 1] = p[i + 1] - value p[i + 1] -= value;
} }
if (p[i + 2] < bcom) { if (p[i + 2] < bcom) {
p[i + 2] = p[i + 2] + value p[i + 2] += value;
} else if (p[i + 2] > bcom) { } else if (p[i + 2] > bcom) {
p[i + 2] = p[i + 2] - value p[i + 2] -= value;
}; }
}; }
}; }
return p; return p;
}; }
function transform_brightness(p, value) { function transform_brightness(p, value) {
var i;
if (value !== 0) { if (value !== 0) {
for (i = 0; i < p.length; i += 4) { for (i = 0; i < p.length; i += 4) {
p[i+0] = p[i+0] + value; //255 = 100% of this color. 255 everything = white. p[i] += value; //255 = 100% of this color
p[i+1] = p[i+1] + value; //if value is negative, add more value to p. if value is positive, subtract value from p p[i + 1] += value;
p[i+2] = p[i+2] + value; p[i + 2] += value;
p[i+3] = p[i+3]; }
}; }
};
return p; return p;
}; }
function transform_comic(p, value) { function transform_comic(p, value) {
var i;
if (value !== 0) { if (value !== 0) {
for (i = 0; i < p.length; i += 4) { for (i = 0; i < p.length; i += 4) {
var frequency = value; p[i] += Math.sin(i * value) * 127 + 128;
p[i + 0] = p[i + 0] + Math.sin(i * frequency) * 127 + 128 p[i + 1] += Math.sin(i * value) * 127 + 128;
p[i + 1] = p[i + 1] + Math.sin(i * frequency) * 127 + 128 p[i + 2] += Math.sin(i * value) * 127 + 128;
p[i + 2] = p[i + 2] + Math.sin(i * frequency) * 127 + 128 }
p[i + 3] = p[i + 3]; }
};
};
return p; return p;
}; }
function transform_duplicate(p, value) { function transform_duplicate(p, value) {
var i;
if (value !== 0) { if (value !== 0) {
for (i = 0; i < p.length; i += 4) { for (i = 0; i < p.length; i += 4) {
p[i + 0] = p[i * value + 0] p[i] = p[i * value];
p[i + 1] = p[i * value + 1] p[i + 1] = p[i * value + 1];
p[i + 2] = p[i * value + 2] p[i + 2] = p[i * value + 2];
p[i + 3] = p[i * value + 3]; p[i + 3] = p[i * value + 3];
}; }
}; }
return p; return p;
}; }
function transform_confetti(p, value) { function transform_confetti(p, value) {
var i;
if (value !== 0) { if (value !== 0) {
for (i = 0; i < p.length; i++) { for (i = 0; i < p.length; i += 1) {
p[i] = Math.sin(value * p[i]) * 127 + p[i] p[i] = Math.sin(value * p[i]) * 127 + p[i];
}; }
}; }
return p; return p;
}; }
if (this.graphicsChanged()) { //operates image pixel manipulation if graphicschanged = true. if (this.graphicsChanged()) {
ctx = canvas.getContext("2d"); ctx = canvas.getContext("2d");
imagedata = ctx.getImageData(0, 0, canvas.width, canvas.height); imagedata = ctx.getImageData(0, 0, canvas.width, canvas.height);
pixels = imagedata.data; pixels = imagedata.data;
// for each effect, do a transform. at any given time, a sprite should wear all 7 effects //A sprite should wear all 7 effects at once
/*pixels = transform_whirl(pixels, this.graphicsValues['whirl']);*/ /*pixels = transform_whirl(pixels, this.graphicsValues.whirl);*/
pixels = transform_negative(pixels, this.graphicsValues['negative']); pixels = transform_negative(pixels, this.graphicsValues.negative);
pixels = transform_brightness(pixels, this.graphicsValues['brightness']); pixels = transform_brightness(pixels, this.graphicsValues.brightness);
pixels = transform_comic(pixels, this.graphicsValues['comic']); pixels = transform_comic(pixels, this.graphicsValues.comic);
/*pixels = transform_pixelate(pixels, this.graphicsValues['pixelate']);*/ /*pixels = transform_pixelate(pixels, this.graphicsValues.pixelate);*/
pixels = transform_duplicate(pixels, this.graphicsValues['duplicate']); pixels = transform_duplicate(pixels, this.graphicsValues.duplicate);
/*pixels = transform_color(pixels, this.graphicsValues['color']);*/ /*pixels = transform_color(pixels, this.graphicsValues.color);*/
/*pixels = transform_fisheye(pixels, this.graphicsValues['fisheye']);*/ /*pixels = transform_fisheye(pixels, this.graphicsValues.fisheye);*/
pixels = transform_confetti(pixels, this.graphicsValues['confetti']); pixels = transform_confetti(pixels, this.graphicsValues.confetti);
//the last object will have all the transformations done on it //the last object will have all the transformations done on it
newimagedata = ctx.createImageData(imagedata); //make new imgdata object newimagedata = ctx.createImageData(imagedata); //make imgdata object
newimagedata.data.set(pixels); //add transformed pixels newimagedata.data.set(pixels); //add transformed pixels
ctx.putImageData(newimagedata, 0, 0); ctx.putImageData(newimagedata, 0, 0);
}; }
return canvas; //for each effect, apply the transformation on the image we receive return canvas;
}; };
SpriteMorph.prototype.setEffect = function (effect, value) { SpriteMorph.prototype.setEffect = function (effect, value) {
@ -2965,8 +2969,8 @@ SpriteMorph.prototype.setEffect = function (effect, value) {
if (eff === 'ghost') { if (eff === 'ghost') {
this.alpha = 1 - Math.min(Math.max(+value || 0, 0), 100) / 100; this.alpha = 1 - Math.min(Math.max(+value || 0, 0), 100) / 100;
} else { } else {
this.graphicsValues[eff] = value; //changes the value of the dictionary this.graphicsValues[eff] = value;
}; }
this.drawNew(); this.drawNew();
this.changed(); this.changed();
}; };
@ -2978,16 +2982,19 @@ SpriteMorph.prototype.getGhostEffect = function () {
SpriteMorph.prototype.changeEffect = function (effect, value) { SpriteMorph.prototype.changeEffect = function (effect, value) {
var eff = effect instanceof Array ? effect[0] : null; var eff = effect instanceof Array ? effect[0] : null;
if (eff === 'ghost') { if (eff === 'ghost') {
this.setEffect(effect, this.getGhostEffect() + (+value || 0)); //special for ghost because of alpha value this.setEffect(effect, this.getGhostEffect() + (+value || 0));
} else { } else {
this.setEffect(effect, this.graphicsValues[eff] + value); this.setEffect(effect, this.graphicsValues[eff] + value);
}; }
}; };
SpriteMorph.prototype.clearEffects = function () { SpriteMorph.prototype.clearEffects = function () {
for (var effect in this.graphicsValues) { var effect;
this.setEffect([effect], 0); for (effect in this.graphicsValues) {
}; if (this.graphicsValues.hasOwnProperty(effect)) {
this.setEffect([effect], 0);
}
}
this.setEffect(['ghost'], 0); this.setEffect(['ghost'], 0);
}; };
@ -4141,7 +4148,7 @@ StageMorph.prototype.init = function (globals) {
this.trailsCanvas = null; this.trailsCanvas = null;
this.isThreadSafe = false; this.isThreadSafe = false;
this.graphicsValues = { 'negative': 0, //dictionary of all the original values this.graphicsValues = { 'negative': 0,
'fisheye': 0, 'fisheye': 0,
'whirl': 0, 'whirl': 0,
'pixelate': 0, 'pixelate': 0,
@ -4151,7 +4158,7 @@ StageMorph.prototype.init = function (globals) {
'comic': 0, 'comic': 0,
'duplicate': 0, 'duplicate': 0,
'confetti': 0 'confetti': 0
}; };
StageMorph.uber.init.call(this); StageMorph.uber.init.call(this);
@ -4217,7 +4224,7 @@ StageMorph.prototype.drawNew = function () {
(this.width() / this.scale - this.costume.width()) / 2, (this.width() / this.scale - this.costume.width()) / 2,
(this.height() / this.scale - this.costume.height()) / 2 (this.height() / this.scale - this.costume.height()) / 2
); );
this.image = this.applyGraphicsEffects(this.image) //apply graphics effects to this image. this.image = this.applyGraphicsEffects(this.image);
} }
}; };