Dojo, Now with Drawing Tools!
Listing 4. Drag-and-Drappable Dominoes
<html>
<head>
<title>Dominoes!</title>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.2/dojo/dojo.xd.js">
</script>
<script type="text/javascript">
dojo.require("dojox.gfx");
dojo.require("dojox.gfx.move");
dojo.addOnLoad(function() {
var node = dojo.byId("surface");
var surface = dojox.gfx.createSurface(node, 600, 300);
/* Using sane ratios for layout, construct a domino */
function drawDomino(surface,x,y,num1,num2,_width) {
var surface = surface.createGroup();
var _width = _width || 200;
var _height = 2*_width, _r = _width/20;
//draw an empty domino...
var rect1 = surface.createRect({
x : x,
y : y,
width : _width,
height : _height,
r : _r
})
.setStroke("black")
.setFill("black")
;
var rect2 = surface.createRect({
x : x+ _r/2,
y : y+ _r/2,
width : _width -_r,
height :_height -_r,
r : _r
})
.setStroke({width: _r/4, color: "white"})
;
var line = surface.createLine({
x1 : x + _r/2,
y1 : y+_height/2,
x2 : x+ _width - _r/2,
y2 : y+_height/2,
})
.setStroke({width: _r/4, color: "white"})
;
//numbers 1,3,5 have dots in the center
if (num1 == 1 || num1 == 3 || num1 == 5) {
surface.createCircle({
cx : x+_width/2,
cy : y+_height/4,
r : _width/10
})
.setStroke("white")
.setFill("white")
;
}
if (num2 == 1 || num2 == 3 || num2 == 5) {
surface.createCircle({
cx : x+_width/2,
cy : y+_height/4*3,
r : _width/10
})
.setStroke("white")
.setFill("white")
;
}
//numbers >= 2 have two of the corners filled in
if (num1 >= 2) {
surface.createCircle({
cx : x+_width/6*5,
cy : y+_height/12,
r : _width/10
})
.setStroke("white")
.setFill("white")
;
surface.createCircle({
cx : x+_width/6,
cy : y+_height/12*5,
r : _width/10
})
.setStroke("white")
.setFill("white")
;
}
/*** SNIP - Go to LJ FTP site to download
the rest of this code (see Resources) ***/
return surface;
}
var width=50,
padding=50;
for (var i=0; i <= 6; i++) {
var d = drawDomino(
surface,
i*75+padding,
2*padding,
i,
Math.floor(Math.random()*7),
width
);
//Make the group drag-and-droppable
new dojox.gfx.Moveable(d);
}
//Adjust the z-index so last domino is on top
dojo.subscribe("/gfx/move/start", function(m) {
m.shape.moveToFront();
});
})
</script>
</head>
<body>
<div id="surface"
style="position:absolute;width:600;height:300;border:solid1px;">
</div>
</body>
</html>
Perhaps the ultimate test of an API is a few good examples of what you can build with it. One of the ultimate demonstrations of gfx's flexibility and power is Dojo's charting library, another DojoX subproject. A comprehensive introduction of the charting library would entail an article of its own, so until that time comes, you can find some great documentation on Dojo charting from the Dojo Key Links page. And, of course, you always can read over the source, which is located in the dojox.charting module of the toolkit's source code, if you want to get an idea of how much work goes into aligning labels, drawing tick marks and so on.
In addition to equipping you with many of the basic charts you'd want to use in a Web application, charting recently got a boost with a number of cool new features, including event support so that custom tooltips and animations can occur within charts—that kind of visual flair makes all the difference. To give you an idea of just how easy the charting API is to get up and running, consider the code blurb in Listing 5 that shows how to create a chart.
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
| Designing Electronics with Linux | May 22, 2013 |
| 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 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Kernel Problem
7 hours 24 min ago - BASH script to log IPs on public web server
11 hours 51 min ago - DynDNS
15 hours 27 min ago - Reply to comment | Linux Journal
15 hours 59 min ago - All the articles you talked
18 hours 23 min ago - All the articles you talked
18 hours 26 min ago - All the articles you talked
18 hours 27 min ago - myip
22 hours 52 min ago - Keeping track of IP address
1 day 43 min ago - Roll your own dynamic dns
1 day 5 hours 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).