Skip to content

Instantly share code, notes, and snippets.

@MangelMaxime
Created July 19, 2016 13:54
Show Gist options
  • Save MangelMaxime/b36a5d0723ffa4c2d1f864ee91380491 to your computer and use it in GitHub Desktop.
Save MangelMaxime/b36a5d0723ffa4c2d1f864ee91380491 to your computer and use it in GitHub Desktop.
Fable : Help for Action
let load tilesheetJsonPath (callback: Action) () =
let mutable tilesheet : TilesheetJson = createEmpty<TilesheetJson>
let loadImg (tilesheet: TilesheetJson) () =
let img = document.createElement_img()
img.onload <- Func<_,_>(fun e ->
let c = document.createElement_canvas()
let ctx = c.getContext_2d()
// Set directly (no css) the width and height of the canvas
// To ensure to draw the whole image. Otherwise, the browsers seems to
// set the size to 300*150
c.width <- img.width
c.height <- img.height
// Store the image in the canvas
ctx.drawImage (U3.Case1 img, 0., 0.)
callback.Invoke()
null
)
img.src <- tilesheet.Tilesheet.Path
ajax (Get tilesheetJsonPath)
(fun data ->
let tilesheet = Serialize.ofJson<TilesheetJson> data
loadImg tilesheet ()
//cache?(tilesheet.Tilesheet.Name) <- tilesheet
)
(fun error ->
console.error ((sprintf "Error when loading %s" tilesheetJsonPath), status))
// Init the cache for the Tilesheet.
let init (tilesheets: string list) () =
for i = 0 to tilesheets.Length do
let current = tilesheets.Item(i)
let cb () =
console.log "coucou"
load current (Action cb) () |> ignore
var load = $exports.load = function (tilesheetJsonPath, callback, unitVar2) {
var tilesheet = {};
var loadImg = function (tilesheet_1) {
return function (unitVar1) {
var img = document.createElement('img');
img.onload = function (e) {
var c, ctx;
return c = document.createElement('canvas'), ctx = c.getContext('2d'), c.width = img.width, c.height = img.height, ctx.drawImage(img, 0, 0), callback(), null;
};
img.src = tilesheet_1.Tilesheet.Path;
};
};
(0, _Utils.ajax)(new _Utils.HttpMethod("Get", tilesheetJsonPath), function (data) {
return function () {
var tilesheet_1;
return tilesheet_1 = _fableCore.Util.ofJson(data), loadImg(tilesheet_1)();
}();
}, function (error) {
console.error(function () {
return function () {
var clo1;
return clo1 = _fableCore.String.fsFormat("Error when loading %s")(function (x) {
return x;
}), function (arg10) {
return clo1(arg10);
};
}();
}()(tilesheetJsonPath), status);
});
};
var init = $exports.init = function (tilesheets, unitVar1) {
for (var i = 0; i <= tilesheets.length; i++) {
var current = _fableCore.Seq.item(i, tilesheets);
var cb = function (unitVar0) {
console.log("coucou");
};
load(current, cb());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment