background preloader

Learning

Facebook Twitter

Creating an image with canvas in Javascript. JavaScript Tutorial 16 - Graphics and Imaging. Graphics brings life to applications. JavaScript allows creation and modification of images using HTML5 canvas objects. This basic tutorial introduces some fundamental concepts of graphics and imaging. A more complete tutorial can be found at Mozilla's Canvas Tutorial. The Canvas HTML 5 introduced the canvas element which allows JavaScript 2D objects to be used in documents. <canvas id="canvas" width='100' height='100'><p>Sorry: Browser does not support Graphics Canvas</p></canvas> Note: Not all browsers implement the canvas element. Shapes and Paths There is only one primitive shape: the rectangle. Paths are used for more complex shapes. Arcs are drawn with arc(x,y,radius,sAng,eAng,rotFlag). sAng and eAng are in radians (note: a simple conversion is var radians=(Math.PI/180)*degrees). rotFlag is boolean.

Bezier curves are used to draw complex shapes. Styles and Patterns Fill patterns control how a drawn object is filled in with ink. Line strokes can be styled in several ways. Using Images. JSApp.US. Engineering: jQuery the Right Way. jQuery has changed the way we write Javascript by abstracting out much of the painful cross-browser implementation details that used to plague developers, but to use it correctly still requires a little knowledge about what’s going on under the hood. In this post we’ll take a good look at jQuery’s selectors and how to use them efficiently. I’ll also talk briefly about DOM manipulation and event handlers. Part 1: Search At its core jQuery is exactly what its name implies, a query engine designed for search.

Ego and #ID You’re right, it’s never that clear cut Good browsers actually provide a getElementsByClass which greatly improves the performance of class selectors. Pseudo selectors provide a lot of power in the right situations but they’re also a lot slower. See? Narrowing the search Good semantics, good selectors In writing your HTML remember that the purpose of IDs are to identify singular elements in your page.

To reduce our search time we need to reduce the search space. Writing with script. Be careful when writing with script. If script is not available, that content will not be created. You should limit your use of this to parts of the page that are not needed to access page content. If you do write important parts of the page that are unavailable without scripting, you should use <noscript> tags to provide an alternative. Writing while the page is still loading If your code is being executed while the page is still loading (in other words, if it is run as part of the initial page layout) put the following: <script type="text/javascript"> document.write('<p>What ever you want to write<\/p>'); document.writeln('<p>What ever you want to write<\/p>'); </script> It will write whatever you put, in whatever part of the page the script is currently running.

Note that if you write content using an event handler (such as the onload handler for an image), it will be treated as if the page has completed loading, even if it has not. Writing after the page has loaded. Google Closure: How not to write JavaScript. At the Edge of the Web conference in Perth last week I got to catch up with Dmitry Baranovskiy, the creator of the Raphaël and gRaphaël JavaScript libraries. Perhaps the most important thing these libraries do is make sophisticated vector graphics possible in Internet Explorer, where JavaScript performance is relatively poor. Dmitry, therefore, has little patience for poorly-written JavaScript like the code he found in Google’s just-released Closure Library. Having delivered a talk on how to write your own JavaScript library (detailed notes) at the conference, Dmitry shared his thoughts on the new library over breakfast the next morning.

“Just what the world needs—another sucky JavaScript library,” he said. For the rest of the day, to anyone who would listen, Dmitry cited example after example of the terrible code he had found when he went digging through Closure. “I’ll make you a deal,” I told him. The Slow Loop From array.js, line 63: for (var i = fromIndex; i < arr.length; i++) { What to Read to Get Up to Speed in JavaScript. There’s a discussion going on on the JSMentors JavaScript mailing list about books to read to get you to the next level . There’s been a lot of great feedback and suggestions thrown out and I wanted to offer up what I felt were good to carry you through the learning process. While I list a number of books in the Big List page that I created , narrowing it down into specific levels makes a lot of sense. Note that some resources will overlap between levels. That should be expected as some books cover a wide breadth of language features.

Also, I am NOT covering blogs in this post, only books (print and online). These are books that I’ve read over the years and found incredibly useful so I’m categorizing them to make it easier for you to get going. Introductory These are books that will give you the fundamentals of the JavaScript language and get you started: JavaScript: The Good Parts Professional JavaScript for Web Developers (Wrox Programmer to Programmer) ppk on JavaScript, 1/e Intermediate.