Last active
May 29, 2016 03:47
-
-
Save nishinoshake/88d54dbe79d0d4533b127b918069980d to your computer and use it in GitHub Desktop.
CreateJSで画像をロードして出力
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
<html> | |
<head> | |
<title>truck</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="https://code.createjs.com/createjs-2015.11.26.min.js"></script> | |
<script src="index.js"></script> | |
<style> | |
body { | |
text-align: center; | |
} | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas id="field" width="1000" height="2000"></canvas> | |
</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
$(function(){ | |
"use strict"; | |
class Field { | |
constructor() { | |
this.$field = $('#field'); | |
this.stage = new createjs.Stage("field"); | |
this.imageQueue = new createjs.LoadQueue(true); | |
this.images = []; | |
this.objects = []; | |
this.initStage(); | |
this.resizeCanvas(); | |
} | |
initStage() { | |
createjs.Ticker.setFPS(60); | |
createjs.Ticker.addEventListener('tick', () => { | |
this.stage.update(); | |
}); | |
} | |
loadImageAsync(images) { | |
let defer = new $.Deferred(); | |
this.images = images; | |
this.imageQueue.loadManifest(images,true); | |
this.imageQueue.addEventListener("complete", () => { | |
defer.resolve(); | |
}); | |
return defer.promise(); | |
} | |
setImages() { | |
this.images.forEach((image) => { | |
let obj = new createjs.Bitmap(image.src); | |
this.objects.push(obj); | |
this.stage.addChild(obj); | |
}); | |
} | |
resizeCanvas() { | |
$(window).on('resize', () => { | |
let ww = $(window).width(), | |
wh = $(window).height(); | |
this.$field.width(ww); | |
this.$field.height(wh); | |
this.$field.attr('width', ww); | |
this.$field.attr('height', wh); | |
}).trigger('resize'); | |
} | |
} | |
let field = new Field(); | |
field.loadImageAsync([{id: 1, src: 'img.png'}]) | |
.done(() => { | |
field.setImages(); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment