Is there a difference between scaling elements in SVG and HTML? - javascript

This question looks similar but ultimately is different and didn't help. Moreover, the accepted answer was written in 2011 and seems outdated. That question has 400+ votes and the accepted answer has 500+ votes so understanding the differences between SVG and HTML is a common problem.
Canvas and its performance advantage is not under consideration because we only need to deal with <= 100 elements at a time, not thousands.
Assume the goal is to create an alternative to Adobe Spark, which is a simple design editor. (See image below.) The editor will let users add, move, and transform elements like text, image, and icons. Designs are output as PNGs.
SVG has worked so far, but there are issues like this and this with computing element positions and sizes.
In preliminary tests, HTML (e.g., div, span, img) looks promising, but before discarding the SVG code, it seems prudent to identify the key differences between HTML and SVG.
In particular:
It seems SVG’s primary difference is “magically” scaling designs from one size to another (e.g., normal resolution -> retina resolution).
But when we manually scale text and image elements with JavaScript, the preliminary tests show the HTML and SVG designs look equally as sharp.
Is there a difference between scaling designs with SVG automatically (by using the width, height, and viewbox attributes on the root SVG element) and scaling divs, spans, text, images, and HTML elements manually with JavaScript?

Related

How much does image size affect CSS rendering / painting performance?

I'm working on JS / HTML game, where all objects are HTML elements (mostly <img>). Their top and left CSS attributes (sometimes along with others, like margins, transformations etc) are dynamically modified by JS code (every frame, basically). I noticed a huge performance improvement, when I switched from using .png files to .gif (22 fps -> 35 fps), but still:
Can further reduction of files' size (by 10-30%) actually noticeably improve CSS transformation performance? I would just test it, but I'm talking about ~250 gif files; and I don't want to loose too much quality, too.
Reduction of the the amount of data(=file size) which will have to be processed, will normally have a positive performance impact, but how much impact it will have has always to be tested and measured, as it depends always on how well your code works together with all surrounding frameworks, the browser and even how the underlying hardware does calculations.
In a worst case where you have to create a lot of resizing an recalulate new pixels, especially if you have to increase the display size of your image again, you might even create a loss in performance.
There is no absolute answer to how good or bad your change will be, until you have tested and measured it in your system yourself.
About Reducing GIF File Size:
A positive factor here is when you cut the width and height of the image by half, the actual file size can be reduced by around 75%.
When using GIF images, reducing the number of different colors used in the GIF image will reduce the size as well. Often you can select the number of colors when exporting gifs from image editor tools. In Photoshop you have an option "Export for Web" which will try to set the optimal settings for usage in web pages.
Regarding Game Performance and Images:
If you already know how you will resize the images(fixed sizes like +50% and +100%), using CSS Image Sprites with pre-resized images can create a good improvement, as you won't have to resize the image, but just use an existing part of the sprite sheet. This will reduce the amount of processing for image manipulation, and therefore increase the performance.
If you want all at once, performance, quality and small file size, the best way to go is replacing as much raster graphics with vector graphics as possible. Most of modern 2D Games use a lot of vector graphics.
Vector graphics are easier to resize, as only the splines have to be recalculated and filled, resizing raster graphics causes calculation for each new pixel.
All common modern browsers support the svg format and can be sized through css. If you want try it out, a free open source tool to create vector graphics is InkScape.
Hope this helps.
Why don't you use JPEG? It's compact. Yes, Image sizes affect performance.
Also, check "CSS Image Sprites", You can have a single image with all your possible icons/images and can handle view using CSS. You'll get better performance as you'll be loading a single image.

SVG performance optimization for browser

I am working on a design tool using SVG. Any other vector formats will be converted to SVG and rendered on the browser with svg.js
I started seeing delays while dragging and moving the elements, when the number of elements increased.
Are there any ways in which I can improve the SVG performance on the browser?
Also, I want give user the ability to rotate and scale elements, render clip arts and add text. Will this make the performance worse?
example
You can see that the drag is pretty slow on that element

Drawing a line between 2 divs

I need to draw a line between 2 divs. I currently use jQuery.
The following is my HTML code. I need to draw a line from the div with id friend1 to the div with id friend2.
<div style="top:30px;left:95px" id="friend1" original-title="Rafael Rosenberg1">
<img src="http://graph.facebook.com/100000796250125/picture" border="0" height="50" width="50"/>
</div>
<div style="top:30px;left:250px" id="friend2" original-title="Rafael Rosenberg2">
<img src="http://graph.facebook.com/100000796250125/picture" border="0" height="50" width="50"/>
</div>
Here is a small library that can draw a line together with jQuery:
http://www.openstudio.fr/Library-for-simple-drawing-with.html
So there are a lot of ways of drawing on a document and it really depends on your actual need.
Plain Old DHTML
It's difficult to draw on a plain HTML document. CSS3 provides some solutions, as you can rotate and I believe it has more transparency features.
But, you COULD create a div that is the length of the distance between the two points (which you can get by your positioning - top right bottom left). Fill this div with a background color and give it some width. Position one end of the div at your first point, then figure out the angle to the second point (also using positioning and some trigonometry), and use CSS3 to rotate the div into position.
Needless to say, techniques like this are cumbersome.
SVG
Vector graphics embedded in your document. You can draw lines easily and apply rotations, as well as add image elements. This would probably be the easiest solution WITH CAVEATS:
SVG is not well supported or documented, so there is a learning curve. Browser support for SVG seems to be increasing, however. IE just started supporting it in version 9.
SVG is an embedded technology and exists in some "viewbox" on your page. So, if you want this to be happening inline with regular HTML elements, it will be more difficult. However, SVG does provide functions that map its internal coordinate system to screen pixels, so it's also doable.
Canvas
Canvas is raster-based graphics embedded in your document. It's good for images, a bit harder for lines, but totally doable given some of the libraries out there. Like SVG, browser support is limited but growing every day, and it also is difficult to interact with the rest of the page in an inline way.
WebGL
OpenGL (3D) for the web. Probably way too heavy for you, but I'll list it for completeness.
here's a link to a gist that uses javascript (jquery) to draw a path (and redraw it in case of window resizing) between any 2 html elements.
demo

How would one create a triangle container for an image (x-browser)

How would I create a DIV containing an IMG where the DIV cuts the image into a triangle, thereby displaying only part of the image though a triangle.
so..
<div>
<img src='some_image' />
</div>
Where the image is a square, but the DIV containing the image is a triangle.
http://www.script-tutorials.com/creating-kaleidoscope-using-jquery-and-css/ solves this very well except this solution is not x-browser friendly (non-ie).
http://css3pie.com/ looks interesting, however this relies on PHP.
You can't create a non-rectangular DOM element.
There are a few ways to hack it.
Firstly, there is a method of using CSS borders with varying widths on each side of the element to make it look triangular. It will still be a rectangle, but it will look like a triangle.
There's a tutorial on this here: http://www.russellheimlich.com/blog/pure-css-shapes-triangles-delicious-logo-and-hearts/
The down-side of this approach is that it is limited to creating right-angled triangles. You can join several of them together to get around this, but then you don't have a single container for your image.
An alternative hacky way of doing it would be to place rotated elements on top of the main element so that they cover the appropriate parts of the image and make it look triangular. This works in all browsers, although IE does have some very nasty syntax to do rotation, and it's quite heavy on the browser, considering that you'd only be using it to make shapes.
Another option might be to use CSS transforms. However this is only supported by a minority of up-to-date browsers, so it won't work for most users.
A better approach might be to use a proper graphics library for this, rather than trying to shoe-horn it into a <div> element.
I'd recommend using Raphael. It's a Javascript library which can draw directly into the browser using SVG (or VML for IE). It's trivial to create triangles using it and to fill them with a graphic. See the examples on the Raphael home page to get you started.
Depending on what you want the outcome to be, as far as i'm aware you cant make a triangle DIV without Transform:; However one solution would be having a div positioned inside the div in question with a PNG cutting of half the image showing only the transparent part through. Not sure if this is a viable option for you though.

Between SVG and canvas, which is better suited for manipulating/animating several images? Maybe neither and just use css3 transforms?

The 2nd part of the question is, which javascript library is better/easier to manipulate images with? I won't be actually drawing any shapes or anything. Other info: I'll be using jQuery and don't need to support all browsers, just webkit.
Edit:
More information: the current design is to layout/draw several rows/columns of images in a grid-like layout, with the image in the center being in "focus" (a little larger, with a border or something and some text next to it). The tricky thing is that we want the whole canvas of images to appear to slide/glide over to bring another random image into focus. So obviously the number of images in this grid needs to exceed what is visible in the viewport so that when the transition occurs there are always images occupying the canvas. Other than moving the images around, I won't be blurring them or otherwise modifying them. Eventually we will add user interactions like clicking/touching on a visible image to bring it to focus manually.
Let me know if this is not clear or still confusing.
I ran across scripty2 which seems like an alternative to using canvas/SVG for my purposes. I also started farting around with EaselJS last night, and it seems like this might work, but I'm wondering if it'll end up being more work/complex than just using standard HTML/CSS and a tool like Scripty2 to aid with animations and click/touch events. Just looking for any suggestions. Thanks!
The answer depends on your manipulation and animation.
If it's just translations, CSS wins for speed compared to canvas. I haven't tested, but I feel confident it easily beats SVG for the same sort of thing.
If you're going to be doing non-affine transformations or otherwise messing with the images (e.g. blurring them) you clearly want Canvas.
If you need event handlers per object, you clearly want a retained-mode drawing system like SVG or HTML+CSS. I haven't done enough CSS3 transforms to say how they compare in terms of speed to SVG, but they clearly do not have the robust transformation DOM of SVG.
This is a rather subjective question (or suite of questions) and you haven't yet given sufficient information for a clear answer to be possible.

Categories