Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scale an SVG <image> while maintaining whole pixels?

I have a pixelart-style image which intend to scale to 4x size with SVG. However, the following code blurs the pixels of the image:

<image x="0" y="0" width="1024" height="1024"
    image-rendering="optimizeSpeed"
    xlink:href="pixelart.bmp" />

Is there some attribute which will resolve this?

like image 212
Eric Avatar asked Jan 04 '11 14:01

Eric


People also ask

Can SVG images be scaled?

Once you add a viewBox to your <svg> (and editors like Inkscape and Illustrator will add it by default), you can use that SVG file as an image, or as inline SVG code, and it will scale perfectly to fit within whatever size you give it.

How do I scale an SVG file?

❓ How can I resize a SVG image? First, you need to add a SVG image file: drag & drop your SVG image file or click inside the white area to choose a file. Then adjust resize settings, and click the "Resize" button. After the process completes, you can download your result file.

Can SVG be stretched?

An SVG image with fixed dimensions will be treated just like a raster image of the same size. Note: If you are trying to stretch your SVG to a different aspect ratio with CSS—for example in order to stretch it over the page background—make sure your SVG includes preserveAspectRatio="none" .

How do I resize an SVG in Inkscape?

Open the SVG icon in the Inkscape. Open File > Document Properties and set width and height to your target size. Now, select all objects in your icon. Adjust the position and size of the object in the top menu bar: For example if you go from viewport 200x200 to viewport to 100x100, divide the value in X, Y, B, W by 2.


1 Answers

I think that image-rendering="optimizeSpeed" is the closest you can get. As the specs for that property state, it "should use a resampling algorithm which achieves the goal of fast rendering, with the requirement that the resampling algorithm shall be at least as good as nearest neighbor resampling." As this is the only section of the spec that mentions "nearest neighbor", I don't think you have any other option.

The only other related thing I can find is the IE9 property -ms-interpolation-mode:nearest-neighbor. This is (of course) IE specific, and is listed as a CSS property (so possibly only applicable to HTML).

Which OS/browser/version is giving you interpolated pixels as a result of upsizing with that attribute?


Also, note that you can use a combination of HTML5 Canvas and SVG to perfectly recreate your image with one SVG <rect> per pixel:
http://phrogz.net/tmp/canvas_image_zoom_svg.xhtml

like image 171
Phrogz Avatar answered Oct 01 '22 11:10

Phrogz