Updated canvg and put PNG export in canvg callback

git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1692 eee81c28-f429-11dd-99c0-75d572ba1ddd
master
Alexis Deveria 2010-08-31 13:44:05 +00:00
parent 560d943769
commit fd5b3f52cf
2 changed files with 65 additions and 42 deletions

View File

@ -31,6 +31,8 @@ if(!Array.indexOf){
// opts: optional hash of options // opts: optional hash of options
// ignoreMouse: true => ignore mouse events // ignoreMouse: true => ignore mouse events
// ignoreAnimation: true => ignore animations // ignoreAnimation: true => ignore animations
// renderCallback: function => will call the function after the first render is completed
// forceRedraw: function => will call the function on every frame, if it returns true, will redraw
this.canvg = function (target, s, opts) { this.canvg = function (target, s, opts) {
if (typeof target == 'string') { if (typeof target == 'string') {
target = document.getElementById(target); target = document.getElementById(target);
@ -69,6 +71,7 @@ if(!Array.indexOf){
svg.Definitions = {}; svg.Definitions = {};
svg.Styles = {}; svg.Styles = {};
svg.Animations = []; svg.Animations = [];
svg.Images = [];
svg.ctx = ctx; svg.ctx = ctx;
svg.ViewPort = new (function () { svg.ViewPort = new (function () {
this.viewPorts = []; this.viewPorts = [];
@ -87,6 +90,14 @@ if(!Array.indexOf){
} }
svg.init(); svg.init();
// images loaded
svg.ImagesLoaded = function() {
for (var i=0; i<svg.Images.length; i++) {
if (!svg.Images[i].loaded) return false;
}
return true;
}
// trim // trim
svg.trim = function(s) { return s.replace(/^\s+|\s+$/g, ''); } svg.trim = function(s) { return s.replace(/^\s+|\s+$/g, ''); }
@ -996,6 +1007,7 @@ if(!Array.indexOf){
this.getMarkers = function() { this.getMarkers = function() {
var markers = []; var markers = [];
for (var i=0; i<this.points.length - 1; i++) { for (var i=0; i<this.points.length - 1; i++) {
markers.push([this.points[i], this.points[i].angleTo(this.points[i+1])]); markers.push([this.points[i], this.points[i].angleTo(this.points[i+1])]);
} }
@ -1777,25 +1789,14 @@ if(!Array.indexOf){
this.base = svg.Element.RenderedElementBase; this.base = svg.Element.RenderedElementBase;
this.base(node); this.base(node);
svg.Images.push(this);
this.img = document.createElement('img'); this.img = document.createElement('img');
this.loaded = false; this.loaded = false;
var that = this; var that = this;
this.renderChildren = function(ctx) { this.img.onload = function() { that.loaded = true; }
if (!this.loaded) { this.img.src = this.attribute('xlink:href').value;
var src = this.attribute('xlink:href').value;
this.img.onload = function() {
that.loaded = true;
that.drawImage(ctx);
}
this.img.src = src;
}
else {
this.drawImage(ctx);
}
}
this.drawImage = function(ctx) { this.renderChildren = function(ctx) {
var x = this.attribute('x').Length.toPixels('x'); var x = this.attribute('x').Length.toPixels('x');
var y = this.attribute('y').Length.toPixels('y'); var y = this.attribute('y').Length.toPixels('y');
@ -1958,33 +1959,54 @@ if(!Array.indexOf){
} }
// bind mouse // bind mouse
ctx.canvas.onclick = function(e) { if (svg.opts == null || svg.opts['ignoreMouse'] != true) {
var p = mapXY(new svg.Point(e != null ? e.clientX : event.clientX, e != null ? e.clientY : event.clientY)); ctx.canvas.onclick = function(e) {
svg.Mouse.onclick(p.x, p.y); var p = mapXY(new svg.Point(e != null ? e.clientX : event.clientX, e != null ? e.clientY : event.clientY));
} svg.Mouse.onclick(p.x, p.y);
ctx.canvas.onmousemove = function(e) { };
var p = mapXY(new svg.Point(e != null ? e.clientX : event.clientX, e != null ? e.clientY : event.clientY)); ctx.canvas.onmousemove = function(e) {
svg.Mouse.onmousemove(p.x, p.y); var p = mapXY(new svg.Point(e != null ? e.clientX : event.clientX, e != null ? e.clientY : event.clientY));
svg.Mouse.onmousemove(p.x, p.y);
};
} }
var dom = svg.parseXml(xml); var dom = svg.parseXml(xml);
var e = svg.CreateElement(dom.documentElement); var e = svg.CreateElement(dom.documentElement);
// set canvas size
if (e.attribute('width').hasValue()) {
ctx.canvas.width = e.attribute('width').Length.toPixels(ctx.canvas.parentNode.clientWidth);
}
if (e.attribute('height').hasValue()) {
ctx.canvas.height = e.attribute('height').Length.toPixels(ctx.canvas.parentNode.clientHeight);
}
svg.ViewPort.SetCurrent(ctx.canvas.clientWidth, ctx.canvas.clientHeight);
// render loop // render loop
ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight); var isFirstRender = true;
e.render(ctx); var draw = function() {
// set canvas size
if (e.style('width').hasValue()) {
ctx.canvas.width = e.style('width').Length.toPixels(ctx.canvas.parentNode.clientWidth);
}
if (e.style('height').hasValue()) {
ctx.canvas.height = e.style('height').Length.toPixels(ctx.canvas.parentNode.clientHeight);
}
svg.ViewPort.SetCurrent(ctx.canvas.clientWidth, ctx.canvas.clientHeight);
// clear and render
ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
e.render(ctx);
if (isFirstRender) {
isFirstRender = false;
if (svg.opts != null && typeof(svg.opts['renderCallback']) == 'function') svg.opts['renderCallback']();
}
}
var waitingForImages = true;
if (svg.ImagesLoaded()) {
waitingForImages = false;
draw();
}
svg.intervalID = setInterval(function() { svg.intervalID = setInterval(function() {
var needUpdate = false; var needUpdate = false;
if (waitingForImages && svg.ImagesLoaded()) {
waitingForImages = false;
needUpdate = true;
}
// need update from mouse events? // need update from mouse events?
if (svg.opts == null || svg.opts['ignoreMouse'] != true) { if (svg.opts == null || svg.opts['ignoreMouse'] != true) {
needUpdate = needUpdate | svg.Mouse.hasEvents(); needUpdate = needUpdate | svg.Mouse.hasEvents();
@ -1997,10 +2019,14 @@ if(!Array.indexOf){
} }
} }
// need update from redraw?
if (svg.opts != null && typeof(svg.opts['forceRedraw']) == 'function') {
if (svg.opts['forceRedraw']() == true) needUpdate = true;
}
// render if needed // render if needed
if (needUpdate) { if (needUpdate) {
ctx.clearRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight); draw();
e.render(ctx);
svg.Mouse.runEvents(); // run and clear our events svg.Mouse.runEvents(); // run and clear our events
} }
}, 1000 / svg.FRAMERATE); }, 1000 / svg.FRAMERATE);

View File

@ -588,10 +588,7 @@
c.width = svgCanvas.contentW; c.width = svgCanvas.contentW;
c.height = svgCanvas.contentH; c.height = svgCanvas.contentH;
canvg(c, data.svg); canvg(c, data.svg, {renderCallback: function() {
// T
setTimeout(function() {
var datauri = c.toDataURL('image/png'); var datauri = c.toDataURL('image/png');
exportWindow.location.href = datauri; exportWindow.location.href = datauri;
var done = $.pref('export_notice_done'); var done = $.pref('export_notice_done');
@ -609,7 +606,7 @@
$.pref('export_notice_done', 'all'); $.pref('export_notice_done', 'all');
exportWindow.alert(note); exportWindow.alert(note);
} }
},1000); }});
}; };
// called when we've selected a different element // called when we've selected a different element