Created
September 28, 2012 09:27
-
-
Save scan/3798853 to your computer and use it in GitHub Desktop.
Javascript to read an image and then cut it into tiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules/ | |
*.sw* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>File API Demo</title> | |
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> | |
<script src="/script.js"></script> | |
</head> | |
<body> | |
<input id="file" name="file" type="file"/> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "file-api", | |
"description": "file api test app", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "3.x", | |
"coffee-script": "1.3.x" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
if not window.File or not window.FileReader or not window.FileList or not window.Blob | |
alert 'The File APIs are not fully supported in this browser.' | |
else | |
($ '#file').change (e) -> | |
file = e.target.files[0] | |
if file.type.match 'image.*' | |
reader = new FileReader | |
reader.onload = do (file) -> | |
(e) -> | |
($ 'body div').remove() | |
src = e.target.result | |
($ '<img>', src: src).appendTo($ 'body').load -> | |
width = ($ @).width() | |
height = ($ @).height() | |
($ @).remove() | |
$d = ($ '<div>').appendTo $ 'body' | |
cols = Math.floor width / 32 | |
rows = Math.floor height / 32 | |
elems = {} | |
tilenum = 0 | |
for y in [0..rows] | |
for x in [0..cols] | |
elems[".tile#{tilenum}"] = | |
x: -x * 32 | |
y: -y * 32 | |
tilenum++ | |
console.log elems | |
style = ".tileset{\n\tbackground-image: url('#{src}'); background-repeat: no-repeat;}.tile{width:32px;height:32px;display:inline-block}\n" | |
for tile of elems | |
t = elems[tile] | |
style += "#{tile} {\n\tbackground-position: #{t.x}px #{t.y}px;\n}\n" | |
$d.append("<style>#{style}</style>") | |
$t = ($ '<div>', style: "width:#{(cols+1)*32}px;") | |
for n in [0..tilenum-1] | |
$t.append $ '<div>', class: "tileset tile tile#{n}" | |
$t.appendTo $d | |
reader.readAsDataURL file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
express = require 'express' | |
coffee = require 'coffee-script' | |
fs = require 'fs' | |
app = express() | |
app.configure -> | |
app.use express.bodyParser() | |
app.use express.methodOverride() | |
app.use express.favicon() | |
app.use express.static "#{__dirname}/public" | |
app.use app.router | |
app.get '/', (req, res) -> | |
res.sendfile "#{__dirname}/index.html" | |
app.get '/script.js', (req, res) -> | |
fs.readFile "#{__dirname}/public/script.coffee", (err, cnt) -> | |
res.set 'Content-Type', 'text/javascript' | |
res.send coffee.compile cnt + '' | |
app.listen 8080 | |
console.log 'Started' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment