Go board with canvas tag

July 7th, 2011 2 comments

Here’s my first attempt at a Go board with javascript and the canvas tag.

<!DOCTYPE html>
<html>
<head>
	<title>Simple Go board with canvas tag</title>
	<script language="JavaScript" type="text/javascript">
<!-- //
	var Go = {
		nextMove : 'black'
	};
 
	Go.board = {
		size : 19, // breaks if > 25
		margin : 30,
		bgColor : '#FFA54F'
	}
 
	Go.apply = function(o, c){
		if(o && c && typeof c == 'object'){
			for(var prop in c){
				o[prop] = c[prop];
			}
		}
		return o;
	};
 
	Go.apply(Go.board, {
		xLabel : 'abcdefghjklmnopqrstuvwxyz',
		map : new Array(),
		stoneWidth : 0,
		stoneHeight : 0,
 
		getStarPoints : function(){
			// I'd like an algorithm for this
			var result = new Array();
			switch (this.size){
				case 19:
					result.push([4,4],[10,4],[16,4],[4,10],[10,10],[16,10],[4,16],[10,16],[16,16]);
					break;
				case 13:
					result.push([4,4],[10,4],[7,7],[4,10],[10,10]);
					break;
				case 9:
					result.push([3,3],[7,3],[5,5],[3,7],[7,7]);
					break;
			}
			return result;
		},
 
		getCoords : function(x,y){
			return this.map[x][y];
		},
 
		buildMap : function(){
			var m = (this.margin||0);
			var hLineGap = (Go.canvas.height-(m*2))/(this.size-1);
			var vLineGap = (Go.canvas.width-(m*2))/(this.size-1);
			var x, y, lat, lon;
 
			// a convenient moment to determine stone size.
			this.stoneWidth  = vLineGap/2;
			this.stoneHeight = hLineGap/2;
 
			for (lon=0; lon<this.size; lon++){
				x = (lon*vLineGap)+m;
				this.map[lon+1] = [];
				for (lat=0; lat<this.size; lat++){
					y = (lat*hLineGap)+m;
					this.map[lon+1][lat+1] = [x,y];
				}
			}
		},
 
		drawBackground : function(){
			var c = Go.cxt;
			c.fillStyle = this.bgColor;
			c.fillRect(0,0,Go.canvas.width, Go.canvas.height);      
		},
 
		drawStone : function(x,y,color){
			var coords = this.getCoords(x,y);
			if (undefined==coords){return false;}
			var c = Go.cxt;
 
			if (!color){
				color = Go.nextMove;
				Go.nextMove = (color=='black')?'white':'black';
			}
 
			c.beginPath();
			c.arc(coords[0],coords[1],this.stoneWidth,0,Math.PI*2,true);
			c.fillStyle = color;
			c.fill();
			c.stroke();
		},
 
		drawStar : function(x,y){
			var coords = this.getCoords(x,y);
			if (undefined==coords){return false;}
			var c = Go.cxt;
 
			c.beginPath();
			c.arc(coords[0],coords[1],4,0,Math.PI*2,true);
			c.fillStyle = 'black';
			c.fill();
			c.stroke();
		},
 
		drawStars : function(){
			var stars = this.getStarPoints();
 
			for (var i=0; i<stars.length; i++){
				star = stars[i];
				this.drawStar(star[0], star[1]);
			}
		},
 
		drawLines : function(){
			var c = Go.cxt, fc, tc;
 
			for (var i=1; i<=this.size; i++){
				c.beginPath();
				fc = this.getCoords(1, i);
				tc = this.getCoords(this.size, i);
				c.moveTo(fc[0], fc[1]);
				c.lineTo(tc[0], tc[1]);
				c.stroke();
			}
 
			for (var i=1; i<=this.size; i++){
				c.beginPath();
				fc = this.getCoords(i, 1);
				tc = this.getCoords(i, this.size);
				c.moveTo(fc[0], fc[1]);
				c.lineTo(tc[0], tc[1]);
				c.stroke();
			}
		},
 
		drawLabels : function(){
			var c = Go.cxt, f1, f2;
 
			c.textAlign = 'center';
			c.textBaseline = 'middle';
 
			for (var i=1; i<=this.size; i++){
				f1 = this.getCoords(i, 1);
				f2 = this.getCoords(i, this.size);
				c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f1[0], f1[1]-(this.margin/2));
				c.strokeText(this.xLabel.charAt(i-1).toUpperCase(), f2[0], f2[1]+(this.margin/2));
			}
 
			for (var i=1; i<=this.size; i++){
				f1 = this.getCoords(1, i);
				f2 = this.getCoords(this.size, i);
				c.strokeText(i, f1[0]-(this.margin/2), f1[1]);
				c.strokeText(i, f2[0]+(this.margin/2), f2[1]);
			}
		},
 
		monitorMouseMove : function(e){
			var x, y, mx, my,
			sw = Go.board.stoneWidth,
			sh = Go.board.stoneHeight;
 
			if(e.offsetX) {
				mx = e.offsetX;
				my = e.offsetY;
			}
			else if(e.layerX) {
				mx = e.layerX;
				my = e.layerY;
			}
 
			x = Math.round((mx+(sw/2))/(2*sw));
			y = Math.round((my+(sw/2))/(2*sh));
		},
 
		monitorMouseClick : function(e){
			// this isn't really perfected..
			var x, y, mx, my,
			sw = Go.board.stoneWidth,
			sh = Go.board.stoneHeight,
			m = Go.board.margin;
 
			if(e.offsetX) {
				mx = e.offsetX;
				my = e.offsetY;
			}
			else if(e.layerX) {
				mx = e.layerX;
				my = e.layerY;
			}
 
			x = Math.round((mx+(m/2)+(sw/2))/(2*sw));
			y = Math.round((my+(m/2)+(sw/2))/(2*sh));
 
			Go.board.drawStone(x,y);
		}
	});
 
	var start = function(){
		Go.canvas = document.getElementById('goboard');
		Go.canvas.onmousemove = Go.board.monitorMouseMove;
		Go.canvas.onclick = Go.board.monitorMouseClick;
		Go.cxt = Go.canvas.getContext('2d');
 
		Go.board.buildMap();
		Go.board.drawBackground();
		Go.board.drawLines();
		Go.board.drawStars();
		Go.board.drawLabels();
	}			
// -->
	</script>
</head>
<body onLoad="start();">
	<canvas id="goboard" name="goboard" width="900" height="900">
	</canvas>
</body>
</html>
Categories: Javascript, 囲碁 Go Tags:

Resetting a Mac’s hardware

March 4th, 2009 No comments

There are a lot of little hidden support features built in to a Mac that you’ll only ever find out if you dig deep or call Apple tech support directly. One quite useful procedure I recently learned of is how to reset all hardware in your mac. This may be useful if you are experiencing odd behavior that doesn’t seem software related.

Step 1: Shut down the Mac normally.
Step 2: Remove all power and peripherals. (Pull every last cord out of the back)
Step 3: Hold the power button in for 10 seconds. (I believe this is to ensure that all power is thoroughly drained from the system as there are certain electronic components that can hold on to a charge for quite some time.)
Step 4: Plug everything back in.
Step 5: Turn on the power and hold command+alt+p+r
Keep these keys held until you hear the boot up chime ring twice. There may be a slight pause between chimes.

Once this is complete, resume using the Mac normally and see if your problems have been solved. (If not, don’t despair. This is after all only one of a number of possible troubleshooting steps)

Categories: Misc Tags: ,

Fantastic Frameworks

February 16th, 2009 No comments

Perhaps I’ll write more about this later, but for now I’ll just say that I’ve been using a lot of the Zend Framework and Ext Javascript library. I enjoy writing code with these fantastic tools.
Zend Framework
Ext Javascript Library

Categories: Javascript, PHP Tags: ,

Useful Javascript – Countdown timer

January 22nd, 2008 No comments

Javascript isn’t always the easiest stuff to write. Browser issues, debugging (at least before the days of firebug), etc. Anybody who’s tried anything more than a simple alert() knows this. So I’m always happy when I come across a free pre-written library for accomplishing tasks that would have otherwise taken me more than a few minutes to do myself. Here’s one that just happened to gracefully meet my every need for a countdown timer:

Countdown Pro

Categories: Javascript Tags:

Dr. Who theme

July 24th, 2007 1 comment

I particularly like the opening theme music to the classic sci-fi television series “Dr. Who”. Here’s how it was made.

UPDATE: It’s gone! youtube took it away! super-drat.

UPDATE 2 I found it again! Here’s the original video I embedded here:

Categories: Misc Tags:

Joost – the future of TV?

May 23rd, 2007 No comments

I’ve recently discovered an interesting new application that claims to be the future of TV: www.joost.com
It’s currently in the beta stages of development and much like when Google’s Gmail first appeared, it’s by invitation only. I happened to come across a blog (by another Matt) who offered free invitations, so I asked and quickly received. (thanks Matt!) Visit his blog at http://whatismatt.com/.

I’m enjoying this program so far. The interface is rather nice, and the channel lineup is growing. Video quality is many times greater than what youtube offers. I can watch it full-screen on my 24-inch iMac and although it’s not quite HD, it’s at least as good as regular television. I imagine as broadband speeds increase, so will the available quality of the stream.

Categories: Misc Tags:

Furigana in Office 2004 for Macintosh

April 18th, 2007 2 comments

I’ve finally discovered the method for enabling furigana in Office 2004 for the Mac.

1) Browse to the following folder in Finder:
“Applications” -> “Microsoft Office 2004″ -> “Additional Tools” -> “Microsoft Language Register”

2) Inside this folder is an application called “Microsoft Language Register”. Do not open this application directly, rather drag and drop the “Microsoft Word” application itself onto the language register program. A prompt will appear allowing you to switch the editing environment to Japanese. (I know this seems a little odd, but apparently it’s the way to do it.)

3) While editing a document, highlight the Kanji you want to apply furigana to and choose Format -> Phonetic guide. Enter the text you want to appear as furigana in the “Ruby Text” field, and hit “OK”.

Categories: Mac, 日本語 Japanese Tags:

KUIS entrance ceremony speech

April 18th, 2007 No comments

On the 4th of March, I gave a speech at the Kanda University of International Studies entrance ceremony.

I’ve given public speeches before, but nothing nearly this size. The auditorium was filled with around 500 people, all in formal attire. And did I mention I gave the speech in Japanese? Just a little pressure there..
Although I did end up slipping up in a couple spots, I think the reception was quite positive overall.

The video was taken by my friend Jani with a little Kodak EasyShare v550 digital camera. Thanks Jani!

Here’s a transcript of the original speech:
Speech Transcript

Categories: 神田外語大学 KUIS Tags:

Full Bloom

April 18th, 2007 No comments

After my parents left for China, I visited Ueno again with my brother, Jani, Yuki, and Tuure. This time the blossoms were in full bloom and I got some pretty good pictures.

3163
3175
Categories: 日本 Japan Tags:

Mt. Fuji

March 28th, 2007 No comments

We managed to catch a good glimpse of Mt. Fuji on the way back from Kyoto today!

2402

Categories: 日本 Japan Tags: