What is "drawing context" exactly? What is the role of getcontext() method? - html

What is getContext() method and what is drawing context exactly? why we always pass the string 2d to the getContext() method?

Context is a way to choose what you are going to do with your canvas.
For moment you can use getContext for 2d (2dcanvas) or for 3d (WebGL).
HTML5 Specification say's about getContext :
"Returns an object that exposes an API for drawing on the canvas. The first argument specifies the desired API. Subsequent arguments are handled by that API."
You can find specifications for each API there :
https://html.spec.whatwg.org/multipage/canvas.html#dom-canvas-getcontext
It is also good to know that "webgl" is the correct name for API but for moment, as it is experimental you should use "experimental-webgl" to start creating WebGL content

In Computer graphics, a drawing context is an abstraction (class/object) that encapsulates how you are going to draw stuff.
At a 100k foot level, computer graphics is about converting drawing commands to pixels (image). How you go from commands to pixels is what the graphics pipeline is all about (very broad and deep subject). A drawing context exposes drawing methods and properties to achieve this.
Example of a drawing commands: drawLine, drawPath, drawRect (you get the idea).
Example of drawing properties: fill color, stroke color, stroke style, font size, clipping region etc
In the context (pardon the pun) of web, you have two drawing contexts - canvas for 2d drawing and and webgl for 3d drawing.

Related

Action Script: Drawing on video

I was wondering how to draw over a video like this.
http://www.bannerblog.com.au/2009/06/burger_king_sharpie.php
I'm not sure that in the example is drawing process over video. As for me It's a 3d model with video background. And you could sync prerecorded states with 3d object transformations (position, rotation, mesh transformations), or create well textured object.
Here for your tutorial how to draw over the 3D objects
Easy! Use Adobe After Effects (or any number of other compositing programs).

Shadow-casting shaders in Stage3D

I've been working a lot with AGAL vertex and fragment shaders. I've got individual objects lit correctly (including specular shading) but I'd like to have objects cast shadows on OTHER objects. I have looked online, but I think most people working directly with AGAL have built custom Stage3D libraries and the shadow-casting solution doesn't seem to be in the public domain. Anyone willing to change that?
I'd like to know how to get an object to cast a shadow on another. I can't post what I've tried, because I can't get my head around where to begin on this problem. How would you pass the information (whether other objects are blocking the light) into another object's shader?
Thanks.
IT's called Deferred shading, you have to do 2 pass of vertex and fragment shaders.
In the first pass you accumulate informations about distances, normals, occlusion...
In the second pass you render and apply the informations of the first pass to make shadows.
Another options is ShadowMapping:
Basic shadowmap
The basic shadowmap algorithm consists in two passes. First, the scene is rendered from the point of view of the light. Only the depth of each fragment is computed. Next, the scene is rendered as usual, but with an extra test to see it the current fragment is in the shadow.
The “being in the shadow” test is actually quite simple. If the current sample is further from the light than the shadowmap at the same point, this means that the scene contains an object that is closer to the light. In other words, the current fragment is in the shadow.

Away3d Camera Path

How to know the coordinates of the path if you know the azimuthal angle. I wanna move my camera around an object in away 3d.
Move camera in Spherical coordinate system by Interpolating from starting azimuthal angle to target - it will be your trajectory.
You can convert from\to Cartesian coordinate system to retrive\pass current coordinate to away3d renderer.
You all so may want to use non-linear camera acceleration(for more realistic feel) it can be implemented with Bézier curves
PS. I will be thankful if your publish you realisation because i will need this functional in my next project. You will save me a lot of time :)
Or just use translation and rotation methods that camera inherits from Object3D it's all covered in Away3D example projects. Allso Away3D has path helper package

How to extract the shape of MovieClip?

We have a flash application that we are planning on converting to javascript. It's a pretty simple map application with an image as the background and a bunch of simple polygon movie clips that represent destinations on the map.
I would like to iterate through each movie clip and extract the shape into an array of x,y points to redraw the polygon using an external javascript function.
Is this possible with actionscript?
If you want to export the shape coordinates at author time, you can do try the JSFL script recommented by #strille or this one or export transparent images (if that's not too limiting for your application).
If you need to export the shapes at runtime, you can use the awesome as3swf library to decompile the swf and export the shapes. Have a look at the ShapeExport wiki as there are couple of handy exporters for js like JSCanvasShapeExporter and the more generic JSONShapeExporter
There are ways you can read the coordinates from an SWF. For instance, I've written a parser in PHP (link). Getting the data doesn't help though, as it turns out. The Flash painting model is different enough from the HTML5 one enough to make transfer exceeding difficult. The main obstacle I discovered is that in Flash, a path can be filled with two fill styles: one for area enclosed by the path, the other for enclosed area considered to be "outside" by the even-odd rule (e.g. the pentagon in the middle of a star). Since the HTML5 canvas let you specify only one fill style, you can't redraw shapes from Flash accurately. I was trying to create a tool that extract shapes as SVG and was getting a lot of gap and holes in the result.
Flash Player 11.6 introduced readGraphicsData() which does exactly what you ask for.
If you need to target an earlier version, then there's no simple way to read shape coordinates from a display object with ActionScript at runtime unfortunately.
If you just want to extract the shape coordinates once someone has written a jsfl script for Flash CS3 which looks like it might be able to help you out.

ActionScript / Flash - Programatically Bitmap-Fill IDE Drawn Vectors?

my current situation maybe akin to me painting myself into a corner.
i have many vector shapes drawn with the Flash Professional CS5 IDE, which have been converted into sprite objects and exported to actionscript. for example, here are 3 shapes:
i want to programatically fill each shape with a bitmap from my library. i realize i can fill these shapes with library bitmaps in the IDE, but i need to scale the bitmaps at runtime as well as swap them out for others.
how is it possible to programatically bitmap-fill shapes drawn within the IDE at runtime without having to also programatically redraw them?
what about using your shapes as masks rather than going through a painful on the fly drawing process ?
it would go like :
bitmap.mask = shape;
as long as shape is a DisplayObject, it should work.
otherwise you can still use a JSFL to export your shapes, store them as arrays of points and draw them at runtime.
here's a basic JSFL export tool
http://ericlin2.tripod.com/bugwire/bugwiret.html
here's an advanced JSFL tool:
http://www.lidev.com.ar/?p=192
here's a ( shamelessly self-promoting :) ) example of an application:
http://en.nicoptere.net/?p=1331
[EDIT]
NB when compiled, your vector shapes are turned into opcode, a set of instructions that you can't read easily.
it remains possible though: http://wahlers.com.br/claus/blog/hacking-swf-1-shapes-in-flash/ but still it's a bit complex if the same result can be achieived with masks :)