Dojo, Now with Drawing Tools!
It won't be long before you'll find that it's far more convenient to transform entire groups of objects instead of applying individual transforms to each object in the group. Let's consider the task of drawing a simple arrow that is nothing more than a line with a triangle on the end of it. Although you could use a path to construct the entire arrow, take a look at how groups can be useful by combining the results from the createLine function and the createPath function (Listing 3).
Listing 3. Arrows in All Four Quadrants
dojo.addOnLoad(function() {
var node = dojo.byId("surface");
var surface = dojox.gfx.createSurface(node, 600, 600)
function drawArrow(p) {
/////////////////////////////////////////////////////
//Create a group that can be manipulated as a whole
/////////////////////////////////////////////////////
var group = surface.createGroup();
var x1 = p.start.x,
y1=p.start.y,
x2 = p.end.x,
y2=p.end.y;
var len = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
var _defaultStroke = {
color : "black",
style : "solid",
width : 1
};
///////////////////////////
//Add a line to the group
///////////////////////////
group.createLine({
x1 : 0,
y1 : 0,
x2 : 0+len,
y2 : 0
})
.setStroke(p.stroke || _defaultStroke)
;
var _arrowHeight = p.arrowHeight || 5;
var _arrowWidth = p.arrowWidth || 3;
/////////////////////////////////////////////////////
//Add a custom path that is a triangle to the group
/////////////////////////////////////////////////////
group.createPath()
.moveTo(len-_arrowHeight,0)
.lineTo(len-_arrowHeight,-_arrowWidth)
.lineTo(len,0)
.lineTo(len-_arrowHeight,_arrowWidth)
.lineTo(len-_arrowHeight,0)
.setStroke(p.stroke || _defaultStroke)
.setFill(p.stroke ? p.stroke.color : "black" )
;
var _rot = Math.asin((y2-y1)/len)*180/Math.PI;
if (x2 <= x1) {_rot = 180-_rot;}
/////////////////////////////////////////////////////////////
//Translate and rotate the entire group as a whole
/////////////////////////////////////////////////////////////
group.setTransform([
dojox.gfx.matrix.translate(x1,y1),
dojox.gfx.matrix.rotategAt(_rot,0,0)
]);
}
//diagonals
drawArrow({start: {x:300,y:300}, end: {x : 435, y : 435}});
drawArrow({start: {x:300,y:300}, end: {x : 165, y : 165}});
drawArrow({start: {x:300,y:300}, end: {x : 435, y : 165}});
drawArrow({start: {x:300,y:300}, end: {x : 165, y : 435}});
//up, down, left, right
drawArrow({start: {x:300,y:300}, end: {x : 300, y : 450}});
drawArrow({start: {x:300,y:300}, end: {x : 300, y : 150}});
drawArrow({start: {x:300,y:300}, end: {x : 150, y : 300}});
drawArrow({start: {x:300,y:300}, end: {x : 450, y : 300}});
});
Attempting to calculate the three points for each of the arrows without the benefit of rotation quickly demonstrates just how laborious high-school geometry really can be; perhaps putting it to work with gfx makes it at least a little more interesting.
Because it's so common to want to interact with graphics, Dojo's gfx library has gone a long way to do most of the legwork for you in this use case as well. To wrap up some aspects of drawing, let's put together a little demonstration that draws a domino on the screen and then add drag-and-drop capabilities to it. As you're about to see, the laborious part of the effort is actually drawing something interesting enough that you'd actually want to drag and drop it. The actual mechanics of making it drag-and-droppable amounts to one whole line of code (Listing 4—note that the full code for Listing 4 is available on LJ's FTP site; see Resources).
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- One Hand Slapping
- Home, My Backup Data Center
- What's the tweeting protocol?
- RSS Feeds
- Readers' Choice Awards 2011
- Trying to Tame the Tablet
- Reply to comment | Linux Journal
4 hours 33 min ago - Reply to comment | Linux Journal
7 hours 5 min ago - Reply to comment | Linux Journal
8 hours 23 min ago - great post
8 hours 58 min ago - Google Docs
9 hours 20 min ago - Reply to comment | Linux Journal
14 hours 9 min ago - Reply to comment | Linux Journal
14 hours 55 min ago - Web Hosting IQ
16 hours 29 min ago - Thanks for taking the time to
18 hours 6 min ago - Linux is good
20 hours 4 min ago









Comments
Please post missing file 10308.tgz
When I try this link in the resources:
ftp://ftp.linuxjournal.com/pub/lj/issue178/10308.tgz
It's a bad link. I found this
ftp://ftp.linuxjournal.com/pub/lj/listings/issue178/README178
10042.tgz -- Web 2.0 Development with the Google Web Toolkit
Federico Kereki
10308.tgz -- Dojo, Now with Drawing Tools!
Matthew Russell
but this file is missing: 10308.tgz
very interesting, good for
very interesting, good for gfx beginner just like me. thanks!
where is the download for listing 4
Trying to download the code from listing 4 from the ftp link in Resources. I get a "550 Failed to change directory" error.
Tried browsing the ftp site, but there seems to be nothing there for this issue (178).