From d1d91c556e1db1cfa327b9bbc9be7484c9512f24 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Sat, 1 Aug 2009 05:03:51 +0000 Subject: [PATCH] Fix several bugs with gradient picking. Uplift to latest jGraduate. git-svn-id: http://svg-edit.googlecode.com/svn/trunk@348 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/jgraduate/jquery.jgraduate.js | 159 +++++++++++++++------------ editor/svg-editor.js | 101 +++++++++++------ editor/svgcanvas.js | 31 +++--- 3 files changed, 172 insertions(+), 119 deletions(-) diff --git a/editor/jgraduate/jquery.jgraduate.js b/editor/jgraduate/jquery.jgraduate.js index 4fe55203..653f1d5b 100644 --- a/editor/jgraduate/jquery.jgraduate.js +++ b/editor/jgraduate/jquery.jgraduate.js @@ -18,13 +18,18 @@ where options is an object literal: } - the Paint object is: - { - // object describing the color picked used by jPicker - solidColor: { hex, }, - // DOM node for the linear gradient - linearGradient: { grad, a, + Paint { + type: String, // one of "none", "solidColor", "linearGradient", "radialGradient" + alpha: Number representing opacity (0-100), + solidColor: String representing #RRGGBB hex of color, + linearGradient: object of interface SVGLinearGradientElement, } -- only one of solidColor and linearGradient must be non-null + +$.jGraduate.Paint() -> constructs a 'none' color +$.jGraduate.Paint({copy: o}) -> creates a copy of the paint o +$.jGraduate.Paint({hex: "#rrggbb"}) -> creates a solid color paint with hex = "#rrggbb" +$.jGraduate.Paint({linearGradient: o, a: 50}) -> creates a linear gradient paint with opacity=0.5 +$.jGraduate.Paint({hex: "#rrggbb", linearGradient: o}) -> throws an exception? - picker accepts the following object as input: { @@ -46,21 +51,44 @@ if(!window.console) { } $.jGraduate = { Paint: - function(copy) { - if (copy) { - if (copy.solidColor) - this.solidColor = new $.jPicker.Color({ hex: copy.solidColor.hex, - a: copy.solidColor.a }); - - // FIXME: linearGradient can be an object, but .grad can still be null - if (copy.linearGradient) - // Opera throws NOT_SUPPORTED_ERROR if the cloneNode(null), the other browsers do not - this.linearGradient = { grad: copy.linearGradient.grad ? document.cloneNode(copy.linearGradient.grad, true) : null, - a: copy.linearGradient.a }; + function(opt) { + var options = opt || {}; + this.alpha = options.alpha || 100; + // copy paint object + if (options.copy) { + this.type = options.copy.type; + this.alpha = options.copy.alpha; + switch(this.type) { + case "none": + this.solidColor = null; + this.linearGradient = null; + break; + case "solidColor": + this.solidColor = options.copy.solidColor; + this.linearGradient = null; + break; + case "linearGradient": + this.solidColor = null; + this.linearGradient = options.copy.linearGradient.cloneNode(true); + break; + } } + // create linear gradient paint + else if (options.linearGradient) { + this.type = "linearGradient"; + this.solidColor = null; + this.linearGradient = options.linearGradient.cloneNode(true); + } + // create solid color paint + else if (options.solidColor) { + this.type = "solidColor"; + this.solidColor = options.solidColor; + } + // create empty paint else { - this.solidColor = new $.jPicker.Color({ hex: '000000', a: 100 }); - this.linearGradient = { grad: null, a: 100 }; + this.type = "none"; + this.solidColor = null; + this.linearGradient = null; } } }; @@ -100,31 +128,19 @@ jQuery.fn.jGraduate = $.extend(true, $this, // public properties, methods, and callbacks { - paint: $settings.paint, + // make a copy of the incoming paint + paint: new $.jGraduate.Paint({copy: $settings.paint}), okCallback: $.isFunction($arguments[1]) && $arguments[1] || null, cancelCallback: $.isFunction($arguments[2]) && $arguments[2] || null, }); - var mode = "solidColor", - pos = $this.position(), + var pos = $this.position(), color = null; - - if ($this.paint == null) { - $this.paint = { solidColor: new $.jPicker.Color({ hex: 'ffffff', a: 100 }), - linearGradient: { grad: null, a: 100 } }; + + if ($this.paint.type == "none") { + $this.paint = $.jGraduate.Paint({solidColor: 'ffffff'}); } - if ($this.paint.linearGradient.grad != null) { - mode = "linearGradient"; - $this.paint.solidColor = new $.jPicker.Color({ hex: 'ffffff', a: 100 }); - } - else if ($this.paint.solidColor != null) { - $this.paint.linearGradient = { grad: null, a: 100 }; - } - else { - return null; - } - $this.addClass('jGraduate_Picker'); $this.html('