Retrieve paths already on HTML canvas - javascript

I am working on a little tool to measure architectural drawings from PDF files. The PDF is rendered by Mozilla pdf.js on to a canvas. Once rendered, I would like to get coordinates all the paths/lines/rects on the canvas. This is to make the tool snap to the endpoints of lines when the mouse cursor is nearby. There don't seem to be an API for retrieving that. As it is not my code that actually renders onto the canvas, I dont path information beforehand.
What I have so far tried is, modify the pdf.js to emit the endpoints' x,y based on moveTo and lineTo, save them in an array. Using this array I can snap correctly in simple drawings. For complex drawings, there seems to be lot of transformations applied and this simple procedure doesnt work.
Any ideas on how to go about it?

Related

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.

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.

Loading a clipped image into a new variable

I'm currently working on a top-down MMORPG, in JavaScript. As you can imagine, this requires a lot of sprite sheets, and herein lies the problem.
I can simply place the clipped version of a sprite sheet onto the canvas using the canvas.drawImage method. However, this must require more performance than simply loading the clipped version of the image into a new image object which I'd then place onto the canvas using the canvas.drawImage method, as I'd only have to clip it once.
Is this at all possible, and if so, how?
It's a sprite. Any "clipping" is a convenient illusion. Loading a clipped version adds more HTTP traffic which will certainly take more time than drawing canvas image, regardless of the image source, especially when you have the image on the client already.

Combine Vector advantages with Bitmap in an HTML canvas element - how?

What I am trying to do is create a game that has an extreme amount of zoom-ability on a canvas element. I would like to make use of the advantage that vector graphics have insofar as being able to be programmatically created at runtime, with the high performance of bitmap images.
What I would like to do is programmatically create the first-frame image of a game "sprite"... this would be a vector image. After the first frame though, I do not want to keep wasting CPU cycles on drawing the image though.. i would like to cache it as a bitmap/high performance image for that zoom level.
Following this, if the user zooms in by >20%, I then redraw the image with a higher level of detail vector image. As above, this vector image would then be cached and optimized.
As you can see here, this would be a pretty basic space ship.. I would first render it programmatically as a vector and then.. raster it I guess? Goal is to avoid wasting CPU.
If the user zooms in...
A new vector image of the same shape would be drawn, albeit with a much higher level of detail. This is basically a Level Of Detail system. In this case as well, after the initial programmatic draw, I would "raster" the image for maximum performance.
Does anyone have ideas on what tools I would need to make this a reality inside of a HTML canvas? (The rest of the game will be running inside of the canvas element..)
Thank you very much for your thoughts.
**Edit: I wanted to add... perhaps the route of rendering an image via SVG (programmatically), then pushing that png file into the canvas using drawimage(), might provide some success? Something similar? Hmm...
Check out that article , but it seems there is no standard method to do what you want and it may fail in IE.
http://svgopen.org/2010/papers/62-From_SVG_to_Canvas_and_Back/#svg_to_canvas
You should perhaps go with an all SVG game , or provide a maximum zooming rate to your game and use big images as sprite assets. it would not have been a problem using flash,but i guess you wont go with flash anyway.
Maybe there is a framework that can translate SVG into a "canvas drawing sequence" but i would not bet on high performances in that case.
I managed to answer my own question.
The way to do this is to first create an SVG file, and then convert it to a PNG file on the client using "canvg". The PNG can be created at different levels of details based on what you want, and in this way you could create a dynamic LOD system.
Flash does something similar automatically by cashing a bitmap image of the SVG file... it's called "pre-rendering". If the SVG isn't scaled or the alpha isn't changed, flash will just use the bitmap instead (much faster then continuously re-rendering the SVG file, in complex cases). Size (and thus detail) of the PNG output can be modified however you like, and so pre-rendering could be done based on events as well.
From this information, I have decided to implement the LOD system such that SVG is used whilst the user is actively zooming (scaling the target "sprite"), and then as the zoom slows down, compute a PNG pre-render. Also, at extremely high levels of zoom, I simply use the SVG, as it is much easier for the CPU to compute SVG's at high resolution, then bitmap images that cover most of the screen. (just take a look at some of the HTML5 icon tests that put lots of icons on the screen... the bigger the icons are, the slower it runs).
Thanks very much to everyone's comments here and I hope that my question/answer has helped someone.

Freeform drawing over an image using <canvas> and javascript

I have been trying to find a good resource to point me in the correct direction and I'd really like someone to help me in this regard.
I'm developing an app that uses phonegap, js and html5. One component of this app is to have an image that can be overlayed with freeform scribbles.
I'm not sure if its the canvas object I should be using and if so how do I go about implementing a drawing solution.
You create a and position it absolute over image.
can have transparent pixels (alpha=1.0) and those pixels will display the underlying image.
Then you flip pixels accordingly your scribbling to black etc. You need to listen to touch events, transform their coordinates to coordinates and then use canvas.getContext() draw operation to manipulate pixels hitting in those coordinates.
If you need further assistance please ask individual questions for each part as a complete solution would be longish source code and outside the scope of simply answering the questions.

Categories

Resources