23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

//Request the next frame<br />

requestAnimationFrame(renderVideo);<br />

Here the page contains a hidden video element (style="display: none") that is told to start playing<br />

once the document is loaded (video1.play()). In a requestAnimationFrame loop, the current frame of<br />

the video is copied to the canvas (drawImage) and the pixels for the frame are copied (getImageData) into<br />

the imgData buffer. We then go through that buffer and negate the color values, thereby producing a<br />

photographically negative image (an alternate formula to change to grayscale is also shown in the code<br />

comments, omitted above). We then copy those pixels back to the canvas (putImageData) so that when<br />

we return, those negated pixels are rendered to the display.<br />

Again, this is processor-intensive as it’s not generally a GPU-accelerated process and might perform<br />

poorly on lower-power devices (be sure, however, to run a Release build outside the debugger when<br />

evaluating erformance). It’s much better to write a video effect DLL where possible as discussed in<br />

“Applying a Video Effect” later on. Nevertheless, it is a useful technique to know. What’s really<br />

happening is that instead of drawing each frame with API calls, we’re simply using the video as a data<br />

source. So we could, if we like, embellish the canvas in any other way we want before returning from the<br />

renderVideo function. An example of this that I really enjoy is shown in Manipulating video using canvas<br />

on Mozilla’s developer site, which dynamically makes green-screen background pixels transparent so<br />

that an img element placed underneath the video shows through as a background. The same could even<br />

be used to layer two videos so that a background video is used instead of a static image. Again, be<br />

mindful of performance on low-power devices; you might consider providing a setting through which<br />

the user can disable such extra effects.<br />

Video Playback and Deferred Loading<br />

Let’s now talk a little more about video playback itself. As we’ve already seen, simply including a video<br />

element in your <strong>HTML</strong> or creating an element on the fly, gives you playback ability. In the code below,<br />

the video is sourced from a local file, starts playing by itself, loops continually, and provides controls:<br />

<br />

As we’ve been doing in this book, we’re not going to rehash the details that are available in the W3C<br />

spec for the video and audio tags, found on http://www.w3.org/TR/html5/video.html. This spec will give<br />

you all the properties, methods, and events for these elements; especially note the event summary in<br />

section 4.8.10.15, and that most of the properties and methods for both are found in Media elements<br />

section 4.8.10. Note that the track element is supported for both video and audio; you can find an<br />

example of using it in Scenario 4 (demonstrating subtitles) of the <strong>HTML</strong> media playback sample. We<br />

won’t be covering it more here.<br />

416

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

Saved successfully!

Ooh no, something went wrong!