Commit c3ecaa81 by Dmitry Baranovskiy

Added rounding of the path to match pixel grid in SVG and fix issue with loosing…

Added rounding of the path to match pixel grid in SVG and fix issue with loosing colour in animation with easing.
parent de55c7fd
...@@ -802,6 +802,23 @@ window.Raphael = (function () { ...@@ -802,6 +802,23 @@ window.Raphael = (function () {
// SVG // SVG
if (R.svg) { if (R.svg) {
var round = function (num) {
return isNaN(num) ? num : +num + ((+num).toFixed() == +num) * .5;
};
var roundPath = function (path) {
for (var i = 0, ii = path.length; i < ii; i++) {
if (path[i][0].toLowerCase() != "a") {
for (var j = 0, jj = path[i].length; j < jj; j++) {
var x = path[i][j];
path[i][j] = round(x);
}
} else {
path[i][6] = round(path[i][6]);
path[i][7] = round(path[i][7]);
}
}
return path;
};
R.toString = function () { R.toString = function () {
return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version; return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version;
}; };
...@@ -812,7 +829,7 @@ window.Raphael = (function () { ...@@ -812,7 +829,7 @@ window.Raphael = (function () {
} }
var p = new Element(el, SVG); var p = new Element(el, SVG);
p.type = "path"; p.type = "path";
p.attrs.path = R.parsePathString(pathString); p.attrs.path = roundPath(pathToAbsolute(pathString));
pathString && p.node.setAttribute("d", p.attrs.path); pathString && p.node.setAttribute("d", p.attrs.path);
setFillAndStroke(p, {fill: "none", stroke: "#000"}); setFillAndStroke(p, {fill: "none", stroke: "#000"});
return p; return p;
...@@ -909,7 +926,7 @@ window.Raphael = (function () { ...@@ -909,7 +926,7 @@ window.Raphael = (function () {
break; break;
case "path": case "path":
if (o.type == "path") { if (o.type == "path") {
attrs.path = R.parsePathString(value); attrs.path = roundPath(pathToAbsolute(value));
node.setAttribute("d", attrs.path); node.setAttribute("d", attrs.path);
} }
case "width": case "width":
...@@ -1047,6 +1064,8 @@ window.Raphael = (function () { ...@@ -1047,6 +1064,8 @@ window.Raphael = (function () {
} }
break; break;
} }
case "font-size":
value = parseInt(value, 10) + "px";
default : default :
var cssrule = att.replace(/(\-.)/g, function (w) { var cssrule = att.replace(/(\-.)/g, function (w) {
return w.substring(1).toUpperCase(); return w.substring(1).toUpperCase();
...@@ -1230,6 +1249,8 @@ window.Raphael = (function () { ...@@ -1230,6 +1249,8 @@ window.Raphael = (function () {
return this; return this;
}; };
var theCircle = function (svg, x, y, r) { var theCircle = function (svg, x, y, r) {
x = round(x);
y = round(y);
var el = doc.createElementNS(svg.svgns, "circle"); var el = doc.createElementNS(svg.svgns, "circle");
el.setAttribute("cx", x); el.setAttribute("cx", x);
el.setAttribute("cy", y); el.setAttribute("cy", y);
...@@ -1249,6 +1270,8 @@ window.Raphael = (function () { ...@@ -1249,6 +1270,8 @@ window.Raphael = (function () {
return res; return res;
}; };
var theRect = function (svg, x, y, w, h, r) { var theRect = function (svg, x, y, w, h, r) {
x = round(x);
y = round(y);
var el = doc.createElementNS(svg.svgns, "rect"); var el = doc.createElementNS(svg.svgns, "rect");
el.setAttribute("x", x); el.setAttribute("x", x);
el.setAttribute("y", y); el.setAttribute("y", y);
...@@ -1277,6 +1300,8 @@ window.Raphael = (function () { ...@@ -1277,6 +1300,8 @@ window.Raphael = (function () {
return res; return res;
}; };
var theEllipse = function (svg, x, y, rx, ry) { var theEllipse = function (svg, x, y, rx, ry) {
x = round(x);
y = round(y);
var el = doc.createElementNS(svg.svgns, "ellipse"); var el = doc.createElementNS(svg.svgns, "ellipse");
el.setAttribute("cx", x); el.setAttribute("cx", x);
el.setAttribute("cy", y); el.setAttribute("cy", y);
...@@ -1460,7 +1485,8 @@ window.Raphael = (function () { ...@@ -1460,7 +1485,8 @@ window.Raphael = (function () {
params.title && (node.title = params.title); params.title && (node.title = params.title);
params.target && (node.target = params.target); params.target && (node.target = params.target);
if (params.path && o.type == "path") { if (params.path && o.type == "path") {
o.node.path = path2vml(params.path); o.attrs.path = R.parsePathString(params.path);
o.node.path = path2vml(o.attrs.path);
} }
if (params.rotation != null) { if (params.rotation != null) {
o.rotate(params.rotation, true); o.rotate(params.rotation, true);
...@@ -2184,19 +2210,6 @@ window.Raphael = (function () { ...@@ -2184,19 +2210,6 @@ window.Raphael = (function () {
paper.text = function (x, y, text) { paper.text = function (x, y, text) {
return theText(this, x, y, text); return theText(this, x, y, text);
}; };
paper.drawGrid = function (x, y, w, h, wv, hv, color) {
color = color || "#000";
var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
rowHeight = h / hv,
columnWidth = w / wv;
for (var i = 1; i < hv; i++) {
path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
}
for (var i = 1; i < wv; i++) {
path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
}
return this.path({stroke: color, "stroke-width": 1}, path.join(","));
};
paper.set = function (itemsArray) { paper.set = function (itemsArray) {
return new Set(itemsArray); return new Set(itemsArray);
}; };
...@@ -2386,7 +2399,6 @@ window.Raphael = (function () { ...@@ -2386,7 +2399,6 @@ window.Raphael = (function () {
clearTimeout(this.animation_in_progress); clearTimeout(this.animation_in_progress);
if (typeof easing == "function" || !easing) { if (typeof easing == "function" || !easing) {
callback = easing || null; callback = easing || null;
easing = "linear";
} }
var from = {}, var from = {},
to = {}, to = {},
...@@ -2445,13 +2457,16 @@ window.Raphael = (function () { ...@@ -2445,13 +2457,16 @@ window.Raphael = (function () {
} }
var start = +new Date, var start = +new Date,
prev = 0, prev = 0,
upto255 = function (color) {
return +color > 255 ? 255 : +color;
},
that = this; that = this;
(function tick() { (function tick() {
var time = new Date - start, var time = new Date - start,
set = {}, set = {},
now; now;
if (time < ms) { if (time < ms) {
pos = R.easing(easing, time / ms); var pos = R.easing(easing, time / ms);
for (var attr in from) { for (var attr in from) {
switch (availableAnimAttrs[attr]) { switch (availableAnimAttrs[attr]) {
case "number": case "number":
...@@ -2459,9 +2474,9 @@ window.Raphael = (function () { ...@@ -2459,9 +2474,9 @@ window.Raphael = (function () {
break; break;
case "colour": case "colour":
now = "rgb(" + [ now = "rgb(" + [
Math.round(from[attr].r + pos * ms * diff[attr].r), upto255(Math.round(from[attr].r + pos * ms * diff[attr].r)),
Math.round(from[attr].g + pos * ms * diff[attr].g), upto255(Math.round(from[attr].g + pos * ms * diff[attr].g)),
Math.round(from[attr].b + pos * ms * diff[attr].b) upto255(Math.round(from[attr].b + pos * ms * diff[attr].b))
].join(",") + ")"; ].join(",") + ")";
break; break;
case "path": case "path":
...@@ -2493,11 +2508,7 @@ window.Raphael = (function () { ...@@ -2493,11 +2508,7 @@ window.Raphael = (function () {
} }
break; break;
} }
if (attr == "font-size") { set[attr] = now;
set[attr] = now + "px";
} else {
set[attr] = now;
}
} }
that.attr(set); that.attr(set);
that.animation_in_progress = setTimeout(tick); that.animation_in_progress = setTimeout(tick);
...@@ -2543,6 +2554,7 @@ window.Raphael = (function () { ...@@ -2543,6 +2554,7 @@ window.Raphael = (function () {
var Set = function (items) { var Set = function (items) {
this.items = []; this.items = [];
this.length = 0; this.length = 0;
arguments.length > 1 && (items = Array.prototype.splice.call(arguments, 0, arguments.length));
if (items) { if (items) {
for (var i = 0, ii = items.length; i < ii; i++) { for (var i = 0, ii = items.length; i < ii; i++) {
if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) { if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment