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.

The MSGestureChange handler for each individual piece (onGestureChange) then takes all the<br />

translation, rotation, and scaling data in the eventArgs object and applies them with <strong>CSS</strong>. This shows<br />

how convenient it is that all those properties are already reported in the coordinate space we need:<br />

function onGestureChange(e) {<br />

var elt = e.target;<br />

var m = new MS<strong>CSS</strong>Matrix(elt.style.msTransform);<br />

}<br />

elt.style.msTransform = m.<br />

translate(e.offsetX, e.offsetY).<br />

translate(e.translationX, e.translationY).<br />

rotate(e.rotation * 180 / Math.PI).<br />

scale(e.scale).<br />

translate(-e.offsetX, -e.offsetY);<br />

There’s a little more going on in the sample, but what we’ve shown here are the important parts.<br />

Clearly, if you didn’t want to support certain kinds of manipulations, you’d again simply ignore certain<br />

properties in the event args object.<br />

Scenario 2 of this sample has the same output but is implemented a little differently. As you can see<br />

in its initialize function (js/gesture.js), the only events that are initially registered apply to the entire<br />

“table top” that contains the black background and a surrounding border. Gesture objects for the<br />

individual pieces are created and attached to a pointer within the MSPointerDown event<br />

(onTableTopPointerDown). This approach is much more efficient and scalable to a puzzle app that has<br />

hundreds or even thousands of pieces, as gesture objects are held only for as long as a particular piece<br />

is being manipulated. Those manipulations are also like those of Scenario 1, where all the<br />

MSGestureChange properties are applied through a <strong>CSS</strong> transform. For further details, refer to the code<br />

comments in js/gesture.js, as they are quite extensive.<br />

Scenario 3 of this sample provides another demonstration of performing translate, pinch-stretch, and<br />

rotate gestures using the mouse wheel. As shown in the PointerEvents example, the only thing you need<br />

to do here is process the wheel event, set eventArgs.pointerId to 1, and pass that onto your<br />

MSPointerDown handler that then adds the pointer to the gesture object:<br />

elt.addEventListener("wheel", onMouseWheel, false);<br />

function onMouseWheel(e) {<br />

e.pointerId = 1; // Fixed pointerId for MouseWheel<br />

onPointerDown(e);<br />

}<br />

Again, that’s all there is to it. (I love it when it’s so simple!) As an exercise, you might try adding this<br />

little bit of code to Scenarios 1 and 2 as well.<br />

The Gesture Recognizer<br />

With inertial gestures, which continue to send some number of MSGestureChange events after pointers<br />

are released, you might be asking this question: What, exactly, controls those events? That is, there is<br />

386

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

Saved successfully!

Ooh no, something went wrong!