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).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
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
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| 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 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- Kernel Problem
2 min 24 sec ago - BASH script to log IPs on public web server
4 hours 29 min ago - DynDNS
8 hours 5 min ago - Reply to comment | Linux Journal
8 hours 37 min ago - All the articles you talked
11 hours 1 min ago - All the articles you talked
11 hours 4 min ago - All the articles you talked
11 hours 5 min ago - myip
15 hours 30 min ago - Keeping track of IP address
17 hours 21 min ago - Roll your own dynamic dns
22 hours 34 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).