23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

document.getElementById("r").style.fill = applyStyles ? "purple" : "";<br />

document.getElementById("l").style.stroke = applyStyles ? "green" : "";<br />

document.getElementById("c").style.fill = applyStyles ? "red" : "";<br />

document.getElementById("t").style.fontStyle = applyStyles ? "normal" : "";<br />

document.getElementById("t").style.textDecoration = applyStyles ? "underline" : "";<br />

The other thing you might have noticed when the styles are applied is that the scaled-up canvas<br />

looks rasterized, like a bitmap would typically be. This is expected behavior, as shown in the following<br />

table of scaling characteristics. These are demonstrated in Scenarios 3 and 4 of the <strong>HTML</strong> Graphics<br />

example.<br />

Element Scaling Handling layout changes for best appearance<br />

img rasterized Change src attribute for different scales (or just use an SVG file as a source).<br />

canvas rasterized Redraw canvas using scaled dimensions; this is often best done by calling .scale<br />

according to the needed display size instead of changing the coordinates used in the code.<br />

svg smooth Not needed. Use viewBox and preseveAspectRatio for proportional scaling.<br />

Additional Characteristics of Graphics Elements<br />

There are a few additional characteristics to be aware of with graphics elements. First, different kinds of<br />

operations will trigger a re-rendering of the element in the document. Second is the mode of operation<br />

of each element. Third are the relative strengths of each element. These are summarized in the<br />

following table:<br />

Element Trigger for re-rendering Mode Strengths<br />

img<br />

Change src attribute<br />

Change of styling via JavaScript<br />

Pixel<br />

Fast to render and transform<br />

Great for static elements and static/repeating<br />

backgrounds<br />

Sprite animation by changing src attribute<br />

Fine-grained dynamic content<br />

Fast to render after being drawn<br />

Pixel-level manipulation<br />

Excellent for fine-grained dynamic/interactive<br />

content with frequent computation<br />

canvas<br />

Calls to context API<br />

Change of styling via JavaScript<br />

Note: re-rendering happens only when<br />

code returns control to the host and<br />

unblocks the UI thread; there are no<br />

visible changes while the code is<br />

manipulating the canvas.<br />

Change to element structure<br />

Change of styling via JavaScript<br />

Immediate: API calls are<br />

rendered to pixels and<br />

forgotten<br />

svg<br />

Retained: all shapes<br />

exist as DOM elements<br />

(unless used as img src)<br />

Smooth scaling<br />

Fine-grained control over individual (retained)<br />

elements<br />

Shape-level manipulation<br />

Excellent for interactive graphics, detailed and<br />

scalable styling, and dynamic per-shape attributes<br />

411

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!