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 * @param {String} prop
* @return {Function} * @return {Function}
* @api private * @api private
*/ */
function normalizedColor(prop) { function getter(prop) {
return function(){ return function(){
var rgba = this[prop]; var val = this[prop];
if (1 == rgba[3]) { if (val instanceof CanvasGradient) return val;
if (1 == val[3]) {
return '#' return '#'
+ rgba[0].toString(16) + val[0].toString(16)
+ rgba[1].toString(16) + val[1].toString(16)
+ rgba[2].toString(16); + val[2].toString(16);
} else { } else {
return 'rgba(' return 'rgba('
+ rgba[0] + ', ' + val[0] + ', '
+ rgba[1] + ', ' + val[1] + ', '
+ rgba[2] + ', ' + val[2] + ', '
+ rgba[3] + ')'; + val[3] + ')';
} }
} }
} }
@ -250,6 +252,7 @@ Context2d.prototype.setTransform = function(){
Context2d.prototype.__defineSetter__('fillStyle', function(val){ Context2d.prototype.__defineSetter__('fillStyle', function(val){
if (val instanceof CanvasGradient) { if (val instanceof CanvasGradient) {
this.lastFillStyle = val;
this.setFillPattern(val); this.setFillPattern(val);
} else if ('string' == typeof val) { } else if ('string' == typeof val) {
var rgba; var rgba;
@ -270,7 +273,7 @@ Context2d.prototype.__defineSetter__('fillStyle', function(val){
* @api public * @api public
*/ */
Context2d.prototype.__defineGetter__('fillStyle', normalizedColor('lastFillStyle')); Context2d.prototype.__defineGetter__('fillStyle', getter('lastFillStyle'));
/** /**
* Set the stroke style with the given css color string. * 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){ Context2d.prototype.__defineSetter__('strokeStyle', function(val){
if (val instanceof CanvasGradient) { if (val instanceof CanvasGradient) {
this.lastStrokeStyle = val;
this.setStrokePattern(val); this.setStrokePattern(val);
} else if ('string' == typeof val) { } else if ('string' == typeof val) {
var rgba; var rgba;
@ -301,7 +305,7 @@ Context2d.prototype.__defineSetter__('strokeStyle', function(val){
* @api public * @api public
*/ */
Context2d.prototype.__defineGetter__('strokeStyle', normalizedColor('lastStrokeStyle')); Context2d.prototype.__defineGetter__('strokeStyle', getter('lastStrokeStyle'));
/** /**
* Set the shadow color with the given css color string. * Set the shadow color with the given css color string.
@ -330,7 +334,7 @@ Context2d.prototype.__defineSetter__('shadowColor', function(val){
* @api public * @api public
*/ */
Context2d.prototype.__defineGetter__('shadowColor', normalizedColor('lastShadowColor')); Context2d.prototype.__defineGetter__('shadowColor', getter('lastShadowColor'));
/** /**
* Set font. * Set font.

6
test/canvas.test.js

@ -125,6 +125,12 @@ module.exports = {
ctx[prop] = 'rgba(128,80,0,0.5)'; 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]); 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