Skip to main content

Coding with Chrome's JavaScript API

Coding with Chrome is a new Google application that's built to help people learn how to code.  It supports Blockly, CoffeeScript, Pencil Code, and JavaScript.  It's free to download and works right your the Chrome browser.

I've been working on some educational material for the new application.  One of the things I really like about it is that it provides a really simple drawing API so you can easily code shapes and drawings instead of starting off with the command line.  Since the application gives you an immediate response, you're able to experiment and see how changing your code affects the picture on the screen.


Here's what I've learned about the basic drawing API.  You can download this file and try experimenting with the code yourself.


/*
 * draw.clear() clears the screen.
 */
draw.clear();

/*
 * draw.point(x, y, color, size) draws the specified point on the screen in the
 * specified color.
 *
 * @param {number} The X-coordinate for the point.
 * @param {number} The Y-coordinate for the point.
 * @param {string} The color for the point. Can be specified as a named color
 *                 or as a hexadecimal color code.
 * @param {number} The size of the point (extends downward and to the right).
 */
draw.point(2, 2, 'purple', 10);

/*
 * draw.rectangle(x, y, width, height, fillcolor, strokecolor, stroke) draws a
 * rectangle on the screen.  The (x, y) coordinate specifies the top-left corner
 * of the rectangle.
 *
 * @param {number} The X-coordinate for the rectangle.
 * @param {number} The Y-coordinate for the rectangle.
 * @param {number} The width of the rectangle.
 * @param {number} The height of the rectangle.
 * @param {string} The fill color of the rectangle. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {string} The stroke color of the rectangle.
 * @param {number} The width of the stroke line.
 */
draw.rectangle(20, 200, 200, 300, 'red', 'green', 5);;
draw.rectangle(100, 400, 40, 100, 'gray', 'black', 1);

/*
 * draw.triangle(x1, y1, x2, y2, x3, y3, fillcolor, strokecolor, stroke) draws
 * a triangle connecting the three points specified.
 *
 * @param {number} The first X-coordinate for the rectangle.
 * @param {number} The first Y-coordinate for the rectangle.
 * @param {number} The second X-coordinate for the rectangle.
 * @param {number} The second Y-coordinate for the rectangle.
 * @param {number} The third X-coordinate for the rectangle.
 * @param {number} The third Y-coordinate for the rectangle.
 * @param {number} The width of the triangle.
 * @param {number} The height of the triangle.
 * @param {string} The fill color of the triangle. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {string} The stroke color of the rectangle.
 * @param {number} The width of the stroke line.
 */
draw.triangle(20, 200, 220, 200, 120, 20, 'blue', 'green', 5);

/*
 * draw.ellipse(x, y, width, height, fillcolor, strokecolor, stroke) draws a
 * ellipse on the screen, with the (x, y) coordinate at the top-left corner of a
 * a hypothetical rectangle that encloses the ellipse.
 *
 * @param {number} The X-coordinate for the ellipse.
 * @param {number} The Y-coordinate for the ellipse.
 * @param {number} The width of the ellipse.
 * @param {number} The height of the ellipse.
 * @param {string} The fill color of the ellipse. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {string} The stroke color of the ellipse.
 * @param {number} The width of the stroke line.
 */
draw.ellipse(70, 260, 40, 60, 'cyan', 'white', 2);
draw.ellipse(170, 260, 40, 60, 'cyan', 'white', 2);

/*
 * draw.circle(x, y, radius, fillcolor, strokecolor, stroke) draws a
 * circle on the screen, with the (x, y) coordinate at the center of the circle.
 *
 * @param {number} The X-coordinate for the circle.
 * @param {number} The Y-coordinate for the circle.
 * @param {number} The radius of the circle.
 * @param {string} The fill color of the circle. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {string} The stroke color of the circle.
 * @param {number} The width of the stroke line.
 */
draw.circle(132, 450, 5, 'white', 'black', 1);

/*
 * draw.line(x1, y1, x2, y2, strokecolor, stroke) draws a line connecting the
 * two specified points.
 *
 * @param {number} The first X-coordinate for the line.
 * @param {number} The first Y-coordinate for the line.
 * @param {number} The second X-coordinate for the line.
 * @param {number} The second Y-coordinate for the line.
 * @param {string} The stroke color of the line. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {number} The width of the stroke line.
 */
draw.line(100, 500, 80, 700, 'black', 1);
draw.line(140, 500, 160, 700, 'black', 1);

/*
 * draw.text(text, x, y, color, drawSmall, drawOutline) draws text on the screen
 * at the specified point. The point marks the bottom-left corner of the text 
 * to be drawn.
 *
 * @param {string} The text to be drawn on the screen.
 * @param {number} The X-coordinate for the text.
 * @param {number} The Y-coordinate for the text.
 * @param {string} The color of the text. Can be specified as a named
 *                 color or as a hexadecimal color code.
 * @param {boolean} If true, draw smaller text.
 * @param {boolean} If true, draw only an outline of the text (no fill).
 */
draw.text('HOUSE', 300, 200, 'black', false, true);





watch my videos on youtube follow me on twitter view my pictures on instagram view my profile on linkedin view my google+ profile

Comments

Popular posts from this blog

The Gobbler from Arby's

Stop.  Stop what you're doing and go to Arby's. Right. Now.  Have them make you a Gobbler .  This is not something you'll regret. Go. Eat this thing. Look at that bacon. Go. Arby's has a new sandwich.  It's called "The Gobbler" and as far as I can tell it's two things: a vehicle for their new deep fried turkey, and an attempt at a Thanksgiving themed sandwich.  It's also a third thing: magically delicious. move over Lucky, there's a new holiday mascot on the block Unwrapping: this actually looks like a sandwich.  It looks appetizing.  It looks like something I want to eat.  It doesn't look like the promo photo above, but it doesn't look like someone was flailing around and accidentally smashed up a sandwich, either. sexy Instagram caption goes here First bite: Wow.  I mean, "WOW."  Holy h*ck this is good.  The turkey has a really bold, meaty flavor.  It tastes a lot like turkey sliced fresh from your

Get Go Sandwich Standoff: "The General" vs "The Rogie Hoagie"

Get Go has been KILLING it lately with crazy sandwiches that are great for advertising on the radio but I've been wondering if they're actually great for eating. The new one I've been hearing about is "The General" which is like Chinese take-out on a sesame sub roll.  I hear ads for it every morning on my commute, and I see a giant billboard for it too.  It's basically chicken tenders with General Tso's sauce and egg rolls on a sesame bun.  I'm guessing they were inspired by Primanti's and decided to try to apply it to a different cuisine (I'm looking forward to The Russian Borscht sub which I'm sure is planned for later this summer). I ventured out to my local GetGo to try one of these out, only to be greeted by "The Rogie Hoagie" on the screen in addition to "The General."  What a great surprise (and additional gastronomic challenge)!  I decided to try them both and report back.  "The General" only comes

Sheetz Sandwich Standoff: El Gringo vs Twisted Swiss

My wife left me alone for dinner tonight so I decided to check out the latest GetGo offerings... but to my great chagrin, they have no promotional subs. My travels led me to the local Sheetz, where I'd be able to keep eating the best gas station sandwiches around. To keep tradition alive, I picked the two most outrageous "Burgerz" on the menu: El Gringo and Twisted Swiss. The ingredient list is promising: Twisted Swiss is the burger with topped with swiss cheese, cole slaw, pickles, bacon, and whatever "Boom Boom Sauce" is on a pretzel bun.  El Gringo is the burger topped with pepper jack cheese, chili, Doritos, and BBQ sauce on a regular old bun. I unwrapped them both and stood back to admire the majesty before me. They're not pretty, but they do look a lot better out of the wrapper than many fast food burgers I've eaten. Twisted Swiss I expected this sandwich to be an awful mess.  It just seemed like a bunch