Browse Source

Fixed getter bug preventing patterns from being returned via fillStyle etc

v1.x
Tj Holowaychuk 14 years ago
parent
commit
fdb626da57
  1. 32
      lib/context2d.js
  2. 6
      test/canvas.test.js

32
lib/context2d.js

@ -53,27 +53,29 @@ var fontre = new RegExp('^ *'
);
/**
* Return a function used to normalize an RGBA color `prop`.
* Return a function used to normalize an RGBA color `prop`
* or the pattern previously set.
*
* @param {String} prop
* @return {Function}
* @api private
*/
function normalizedColor(prop) {
function getter(prop) {
return function(){
var rgba = this[prop];
if (1 == rgba[3]) {
var val = this[prop];
if (val instanceof CanvasGradient) return val;
if (1 == val[3]) {
return '#'
+ rgba[0].toString(16)
+ rgba[1].toString(16)
+ rgba[2].toString(16);
+ val[0].toString(16)
+ val[1].toString(16)
+ val[2].toString(16);
} else {
return 'rgba('
+ rgba[0] + ', '
+ rgba[1] + ', '
+ rgba[2] + ', '
+ rgba[3] + ')';
+ val[0] + ', '
+ val[1] + ', '
+ val[2] + ', '
+ val[3] + ')';
}
}
}
@ -250,6 +252,7 @@ Context2d.prototype.setTransform = function(){
Context2d.prototype.__defineSetter__('fillStyle', function(val){
if (val instanceof CanvasGradient) {
this.lastFillStyle = val;
this.setFillPattern(val);
} else if ('string' == typeof val) {
var rgba;
@ -270,7 +273,7 @@ Context2d.prototype.__defineSetter__('fillStyle', function(val){
* @api public
*/
Context2d.prototype.__defineGetter__('fillStyle', normalizedColor('lastFillStyle'));
Context2d.prototype.__defineGetter__('fillStyle', getter('lastFillStyle'));
/**
* Set the stroke style with the given css color string.
@ -281,6 +284,7 @@ Context2d.prototype.__defineGetter__('fillStyle', normalizedColor('lastFillStyle
Context2d.prototype.__defineSetter__('strokeStyle', function(val){
if (val instanceof CanvasGradient) {
this.lastStrokeStyle = val;
this.setStrokePattern(val);
} else if ('string' == typeof val) {
var rgba;
@ -301,7 +305,7 @@ Context2d.prototype.__defineSetter__('strokeStyle', function(val){
* @api public
*/
Context2d.prototype.__defineGetter__('strokeStyle', normalizedColor('lastStrokeStyle'));
Context2d.prototype.__defineGetter__('strokeStyle', getter('lastStrokeStyle'));
/**
* Set the shadow color with the given css color string.
@ -330,7 +334,7 @@ Context2d.prototype.__defineSetter__('shadowColor', function(val){
* @api public
*/
Context2d.prototype.__defineGetter__('shadowColor', normalizedColor('lastShadowColor'));
Context2d.prototype.__defineGetter__('shadowColor', getter('lastShadowColor'));
/**
* Set font.

6
test/canvas.test.js

@ -125,6 +125,12 @@ module.exports = {
ctx[prop] = 'rgba(128,80,0,0.5)';
assert.equal('rgba(128, 80, 0, 0.5)', ctx[prop], prop + ' rgba(128,80,0,0.5) -> rgba(128, 80, 0, 0.5), got ' + ctx[prop]);
if ('shadowColor' == prop) return;
var grad = ctx.createLinearGradient(0,0,0,150);
ctx[prop] = grad;
assert.strictEqual(grad, ctx[prop], prop + ' pattern getter failed');
});
},

Loading…
Cancel
Save