1 | /*
|
---|
2 | * ext-eyedropper.js
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2
|
---|
5 | *
|
---|
6 | * Copyright(c) 2010 Jeff Schiller
|
---|
7 | *
|
---|
8 | */
|
---|
9 |
|
---|
10 | // Dependencies:
|
---|
11 | // 1) jQuery
|
---|
12 | // 2) history.js
|
---|
13 | // 3) svg_editor.js
|
---|
14 | // 4) svgcanvas.js
|
---|
15 |
|
---|
16 | methodDraw.addExtension("eyedropper", function(S) {
|
---|
17 | var svgcontent = S.svgcontent,
|
---|
18 | svgns = "http://www.w3.org/2000/svg",
|
---|
19 | svgdoc = S.svgroot.parentNode.ownerDocument,
|
---|
20 | svgCanvas = methodDraw.canvas,
|
---|
21 | ChangeElementCommand = svgedit.history.ChangeElementCommand,
|
---|
22 | addToHistory = function(cmd) { svgCanvas.undoMgr.addCommandToHistory(cmd); },
|
---|
23 | currentStyle = {fillPaint: "red", fillOpacity: 1.0,
|
---|
24 | strokePaint: "black", strokeOpacity: 1.0,
|
---|
25 | strokeWidth: 5, strokeDashArray: null,
|
---|
26 | opacity: 1.0,
|
---|
27 | strokeLinecap: 'butt',
|
---|
28 | strokeLinejoin: 'miter'
|
---|
29 | };
|
---|
30 | function getStyle(opts) {
|
---|
31 | // if we are in eyedropper mode, we don't want to disable the eye-dropper tool
|
---|
32 | var mode = svgCanvas.getMode();
|
---|
33 | if (mode == "eyedropper") return;
|
---|
34 | var tool = $('#tool_eyedropper');
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | var getPaint = function(color, opac, type) {
|
---|
39 | // update the editor's fill paint
|
---|
40 | var opts = null;
|
---|
41 | if (color.indexOf("url(#") === 0) {
|
---|
42 | var refElem = svgCanvas.getRefElem(color);
|
---|
43 | if(refElem) {
|
---|
44 | refElem = refElem.cloneNode(true);
|
---|
45 | } else {
|
---|
46 | refElem = $("#" + type + "_color defs *")[0];
|
---|
47 | }
|
---|
48 |
|
---|
49 | opts = { alpha: opac };
|
---|
50 | opts[refElem.tagName] = refElem;
|
---|
51 | }
|
---|
52 | else if (color.indexOf("#") === 0) {
|
---|
53 | opts = {
|
---|
54 | alpha: opac,
|
---|
55 | solidColor: color.substr(1)
|
---|
56 | };
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | opts = {
|
---|
60 | alpha: opac,
|
---|
61 | solidColor: 'none'
|
---|
62 | };
|
---|
63 | }
|
---|
64 | return new $.jGraduate.Paint(opts);
|
---|
65 | };
|
---|
66 |
|
---|
67 | return {
|
---|
68 | name: "eyedropper",
|
---|
69 | svgicons: "extensions/eyedropper-icon.xml",
|
---|
70 | buttons: [{
|
---|
71 | id: "tool_eyedropper",
|
---|
72 | type: "mode",
|
---|
73 | title: "Eye Dropper Tool",
|
---|
74 | position: 8,
|
---|
75 | key: "I",
|
---|
76 | icon: "extensions/eyedropper.png",
|
---|
77 | events: {
|
---|
78 | "click": function() {
|
---|
79 | svgCanvas.setMode("eyedropper");
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }],
|
---|
83 |
|
---|
84 | mouseDown: function(opts) {
|
---|
85 | var mode = svgCanvas.getMode();
|
---|
86 | var e = opts.event;
|
---|
87 | var target = (e.target.id === "svgroot") ? document.getElementById('canvas_background') : e.target;
|
---|
88 | if (mode == "eyedropper" && target) {
|
---|
89 | currentStyle.fillPaint = target.getAttribute("fill") || "white";
|
---|
90 | currentStyle.fillOpacity = target.getAttribute("fill-opacity") || 1.0;
|
---|
91 | currentStyle.strokePaint = target.getAttribute("stroke") || 'none';
|
---|
92 | currentStyle.strokeOpacity = target.getAttribute("stroke-opacity") || 1.0;
|
---|
93 | currentStyle.strokeWidth = target.getAttribute("stroke-width");
|
---|
94 | currentStyle.strokeDashArray = target.getAttribute("stroke-dasharray");
|
---|
95 | currentStyle.strokeLinecap = target.getAttribute("stroke-linecap");
|
---|
96 | currentStyle.strokeLinejoin = target.getAttribute("stroke-linejoin");
|
---|
97 | currentStyle.opacity = target.getAttribute("opacity") || 1.0;
|
---|
98 | opts.selectedElements = opts.selectedElements.filter(Boolean)
|
---|
99 | if (!opts.selectedElements.length) { //nothing selected, just update colors
|
---|
100 | var fill = getPaint(currentStyle.fillPaint, currentStyle.fillOpacity*100, "fill");
|
---|
101 | var stroke = getPaint(currentStyle.strokePaint, currentStyle.strokeOpacity*100, "stroke");
|
---|
102 | methodDraw.paintBox.fill.setPaint(fill)
|
---|
103 | methodDraw.paintBox.stroke.setPaint(stroke)
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | if ($.inArray(opts.selectedElements.nodeName, ['g', 'use']) == -1) {
|
---|
107 | var changes = {};
|
---|
108 | var change = function(elem, attrname, newvalue) {
|
---|
109 | changes[attrname] = elem.getAttribute(attrname);
|
---|
110 | elem.setAttribute(attrname, newvalue);
|
---|
111 | };
|
---|
112 | var batchCmd = new S.BatchCommand();
|
---|
113 | opts.selectedElements.forEach(function(element){
|
---|
114 | if (currentStyle.fillPaint) change(element, "fill", currentStyle.fillPaint);
|
---|
115 | if (currentStyle.fillOpacity) change(element, "fill-opacity", currentStyle.fillOpacity);
|
---|
116 | if (currentStyle.strokePaint) change(element, "stroke", currentStyle.strokePaint);
|
---|
117 | if (currentStyle.strokeOpacity) change(element, "stroke-opacity", currentStyle.strokeOpacity);
|
---|
118 | if (currentStyle.strokeWidth) change(element, "stroke-width", currentStyle.strokeWidth);
|
---|
119 | if (currentStyle.strokeDashArray) change(element, "stroke-dasharray", currentStyle.strokeDashArray);
|
---|
120 | if (currentStyle.opacity) change(element, "opacity", currentStyle.opacity);
|
---|
121 | if (currentStyle.strokeLinecap) change(element, "stroke-linecap", currentStyle.strokeLinecap);
|
---|
122 | if (currentStyle.strokeLinejoin) change(element, "stroke-linejoin", currentStyle.strokeLinejoin);
|
---|
123 | batchCmd.addSubCommand(new ChangeElementCommand(element, changes));
|
---|
124 | changes = {};
|
---|
125 | });
|
---|
126 | var fill = getPaint(currentStyle.fillPaint, currentStyle.fillOpacity*100, "fill")
|
---|
127 | var stroke = getPaint(currentStyle.strokePaint, currentStyle.strokeOpacity*100, "stroke")
|
---|
128 | methodDraw.paintBox.fill.update(true)
|
---|
129 | methodDraw.paintBox.stroke.update(true)
|
---|
130 | addToHistory(batchCmd);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | };
|
---|
135 | }); |
---|