Don E Posted July 3, 2017 Share Posted July 3, 2017 Hello everyone, I have a question about the HTML canvas element. If loading an image into a canvas where the canvas has been scaled down to a smaller size because of responsive design, say for example 300 by 300, how can then after playing in the canvas get the original size of the image for the canvas upon out putting to the browser to download? So for example, original image size may be: 600x600. Loading it into a canvas that is being rendered responsive is scaled to: 300x300. Upon finishing playing with the image in the canvas, how can I get a 600x600 version of it instead of the scaled size which is 300x300? I've noticed that whatever the canvas width and height is set to, you will get that size for your output, which is fine but if an image is loaded into the canvas, it would be nice upon output to output as the original width and height for the canvas. Appreciate it in advance. Thanks! Link to comment Share on other sites More sharing options...
dsonesuk Posted July 3, 2017 Share Posted July 3, 2017 Once an image is placed in the canvas, its no longer related to the original image, its an object placed on canvas and now just a part of the canvas only. Its like printing an image on a a4 sheet of paper, you can't link to original image. The whole A4 or canvas is now a image of sorts. Apparently you can add hyperlinks, https://stackoverflow.com/questions/6215841/create-links-in-html-canvas so you could create link to original image, OR if it is a single canvas image, place anchor hyperlink to original image around whole canvas. Link to comment Share on other sites More sharing options...
Don E Posted July 3, 2017 Author Share Posted July 3, 2017 I see, but is it possible to output the canvas(in this case an image that has lines drawn on it) to the browser with the original dimensions(width and height) of the image? So if the canvas is rendered down to 300x300 because of responsive design and we load an image into it, yes the image takes on the dimensions of the canvas which would be 300x300 but I was wondering if we can output the edited canvas image back out to 600x600 (original dimensions)? For i.e., the A4(smaller) print now can be printed into a poster(bigger) for example. Would be nice if we can take a canvas at a certain size and output it as a different size(bigger in this case) and still maintain all it's data. Link to comment Share on other sites More sharing options...
dsonesuk Posted July 3, 2017 Share Posted July 3, 2017 Why not create pop-up of original size if clicked? OR convert canvas to image see https://davidwalsh.name/convert-canvas-image Link to comment Share on other sites More sharing options...
Ingolme Posted July 3, 2017 Share Posted July 3, 2017 A responsive page should not have any effect on the canvas. The width and height attributes of the canvas determine its real size, if it has been scaled down by CSS that will not actually reduce the number of pixels that are on the canvas. Link to comment Share on other sites More sharing options...
Don E Posted July 3, 2017 Author Share Posted July 3, 2017 4 hours ago, Ingolme said: A responsive page should not have any effect on the canvas. The width and height attributes of the canvas determine its real size, if it has been scaled down by CSS that will not actually reduce the number of pixels that are on the canvas. Yes you're correct. The only thing with that is, if I draw on the canvas where the canvas has been scaled down by CSS, you get the result below(see link), the mouse is not matching with the drawn lines. This is expected because the actual drawing is happening on the canvas's width and height dimensions which in this case is: 670x470 but the scaling is: 255x168 Is there anyway to have the mouse match up with the drawing when the image is scaled? Video Demo Thanks. Link to comment Share on other sites More sharing options...
Ingolme Posted July 3, 2017 Share Posted July 3, 2017 I'm not sure what code you're using to get the mouse offset, but once you have it you can calculate the correct position by dividing the canvas real size by its visible size. // Assuming we already have a reference to canvas and the mouse's position // relative to it is in visibleX and visibleY var horizontalRatio = canvas.width / canvas.offsetWidth; var verticalRatio = canvas.height / canvas.offserHeight; var realX = horizontalRatio * visibleX; var realY = verticalRatio * visibleY; // Do something with realX and realY 1 Link to comment Share on other sites More sharing options...
Don E Posted July 4, 2017 Author Share Posted July 4, 2017 Thanks Foxy Mod for that code snippet, was able to figure out. Also thanks dsonesuk. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now