Merge SVG and .JPG into one image? - javascript

I have an website using SVG/VML (via Raphael JS) setup in a mapping application where the SVG is used to display graphics atop a backdrop map image. This works very well onscreen, and for printing hardcopy maps with overlays. Where this setup falls apart however is when the user wants to save the map image with the SVG overlay to a local .JPG file.
More specifically, using the standard right-click functionality of most browsers to "Save image as..." does not work when there is an SVG/VML element sitting atop the image. Right-click on the map, and the user can save the map image, but without the overlay. Right-click on the overlaid SVG element, and the best the user gets is the ability to inspect the element or save out some HTML (it varies by browser).
So my main question here is; Is it possible to take an image and an SVG element and combine them (preferably client-side, though I'm open to options) into one "flattened" image, .JPG, .PNG or otherwise, that can then be right-clicked and saved, or downloaded to a user's PC upon request?

What you need to do is, instead of overlaying JPG and SVG:
Take the original image
Draw the lines in the SVG file on it in memory
Output that single image as a JPG to the browser.
This of course means that you have to be able to interpret SVG...

One possibility would be to embed the image into the SVG, and then generate a Data URL with the combined images. The following example achieves this in png format:
var datauri = c.toDataURL('image/png');
where c is your formatted SVG layer. For more info, check out this open source editor, http://code.google.com/p/svg-edit/, in the svgcanvas.js and svg-editor.js files is a good example of how to export SVG as a png file. Its a little hard to understand at first, but very well written.

I'm afraid you're gonna have a tough time attempting to render SVG on a Canvas element just like that due to security constraints (I could not get this working in Firefox at all for instance).
Here's an idea though:
Place your image inside the SVG DOM using the svg <image> tag
Pass your SVG code through the canvg library
Use the toDataURL method of canvg to generate the image

Related

Layering The Canvas Without Multiple Canvases

There I am creating a stamp maker in HTML,Jquery and javascript. In the editor of my application image is adding to HTML canvas (simple) and also the text, I just create new line element and then append it to the canvas.But the problem is that I want my image to be in back of text. I googled it alot and also search stackoverflow I got the solution of creating multiple canvases but in last I have to download the canvas to file for user. There is the problem. And I want to export whole canvas along with text one and second image together.If I create seperate canvas for the text and another for image and give image one low zindex it would be fine but there will be one canvas to be exported as image.
Link to multiple layers in canvas html5 - canvas element - Multiple layers
I am hopping that we would come up with an idea of how to download both canvases as an image or find a method to take image to back of the canvas.
Any answer would be appreciated.
If you store the text, second image and first image in variables, you could just draw them on the canvas in the order you prefer. This means that, whenever there's some change in the image or text, you should clean the canvas and redraw everything.
Also, you may be interested in Compositing, since it allows you to decide where a new object is drawn in relation to the existing drawing (on top, behind, etc.)

Creating a HTML5 - Image Editor which uses SVG

I'm trying to create a simple 'image editor' using HTML5/Javascript that allows to load an image (.png for instance), draw/move/delete different shapes onto that image, and retrieve the coordinates of these shapes drawn to modify the original image.
I've been reading quite a lot regarding SVG and canvas, and my final thought was to go with SVG, as it's handling all the different object states etc. Looking at different examples of code though I'm not sure anymore if SVG is the right way to go...is it even possible to accomplish what I want using SVG?
Update:
The problem is basically to ensure that the chosen strategy (SVG/canvas) supports loading an image, drawing additional shapes, and retrieving the coordinates in a way that modifications on the original image result in the same picture as shown in the editor.

SVG file : How to get Path markup datas after convert a PNG file into a SVG file with Inkscape?

I'm asking if there would be a tool to convert a PNG file into a SVG file where I could get the Path markup. I have tried with Inkscape but when I edit the SVG file I can't find the path markup, there is only "binary" datas.
SVG file path markup example :
https://www.w3.org/TR/SVG/paths.html#PathData
Is anyone have a idea ?
What do you mean by "convert a PNG file into a SVG file"? It sounds like you might just be putting your PNG into an SVG file. That doesn't automatically turn it into a path. It is just a bitmap embedded in an SVG. It stays a bitmap.
What it sounds like you want, is to convert your bitmap to a vector. That process is commonly called a "bitmap trace". Inkscape has a feature to do that. Select the PNG and do a Path->Trace Bitmap...
How well it works will depend on your image. Logos generally work well. Photos usually don't.
Once you have done the trace, save the SVG file and you will be able to get the path data then.
You can't make a raster image into a vector image. Though there are programs like vector magic that try to make it into a vector image looking as close as possible as the raster image by looking at shapes of similar colors.

Store and load fabricjs canvas on server

I am creating an app which uses fabricjs canvas drawing comics. Canvas contains some objects such as rectangles, svg object, and jpeg images. After editing is done, the canvas need to be stored in the server (nodejs) and should be reloaded later from server by the creator or other viewers.
I thought I can serialize canvas to svg string and store svg string on the server and upload images which exist in the canvas one by one to the server as well. Later on, load the svg string replace the images url with the downloaded picture and reload the canvas.
Here are my questions:
1- Is this the right way to achieve my goal?
2- I am doing all of this because my canvas will have different size depending on user device (monitor, smartphone,...) therefore, I chose to use svg for scalability. However, I'm not sure how to scale the original svg string to the new canvas.
Thanks,
Don't save to SVG. SVG is for export. Call myCanvas.toObject()
Not sure how you're going to take advantage of SVG's scalability, if you are just using SVG as a persistence format for canvas. But if you're going to render in SVG, then that makes sense.
You may be overthinking the image solution. How are users going to put an image on the canvas in the first place? The way I do it is to have them upload the image to my server. Then I let them choose images from an image browser. The image they choose already has a URL, so there's nothing left to do when storing/restoring.

HTML 5 Canvas, Edit and save on existing image

Being Super new to HTML 5 Canvas, I need to be able to load an image, and then be able to draw ie "hit points" on the image and save.
Ive only just started and can do this
http://www.w3schools.com/html5/canvas_drawimage.asp
so thats fine loading the image I just now need to allow the user to click on parts of the image to create dots and then be able to save them. Any good links or examples?
Opera has a good tutorial on letting the user "paint" on the canvas: http://dev.opera.com/articles/view/html5-canvas-painting/
For the graphic editing of the dots there are IMO three main options:
load the image in a canvas and really change the canvas by drawing on it
load the image in an img and use it as a background over which there is a transparent canvas that is where you draw the dots
don't touch the image and don't use a canvas but make each dot a separate img tag that simply lives on top of the image
For sending the result you probably don't want to send over the modified image, but just the selected dot configuration and that is a normal ajax request to the server. It is also possible to send the modified image in png format (or even to "downloading" it locally on the computer without sending anything to the server), but that is IMO not an useful capability in this case.
Not exactly the same, but you can achieve that. I can give you an headstart on this - checkout this jsFiddle. I built this editor using FabricJS. The save functionality is also given in the NoteEditor.js
var canvas = new fabric.Canvas('c');
var imgInstance = new fabric.Image(imgElement);
canvas.add(imgInstance);//initialize the Canvas with the image

Categories

Resources