Skip to content

Commit 37cb069

Browse files
committed
Added example of using Image apis and event loading
1 parent 2a3dd9e commit 37cb069

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

examples/image_examples.res

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// question from the forum https://forum.rescript-lang.org/t/how-to-get-img-dimmensions-using-rescript/3556?u=spyder
2+
let url = ""
3+
4+
open Webapi.Dom
5+
6+
let processImage = url => {
7+
Promise.make((resolve, reject) => {
8+
// showing the process, this can be done more easily (see below)
9+
let e = document->Document.createElement("img")
10+
let maybeImg = HtmlImageElement.ofElement(e)
11+
let img = Belt.Option.getUnsafe(maybeImg)
12+
13+
img->HtmlImageElement.setSrc(url)
14+
img->HtmlImageElement.addLoadEventListener(event => resolve(. event))
15+
img->HtmlImageElement.addErrorEventListener(error => reject(. error))
16+
17+
let body =
18+
document
19+
->Document.asHtmlDocument
20+
->Belt.Option.flatMap(HtmlDocument.body)
21+
->Belt.Option.getUnsafe
22+
23+
body->Element.appendChild(~child=img->HtmlImageElement.asNode)
24+
body->Element.removeChild(~child=img->HtmlImageElement.asNode)->ignore
25+
})
26+
}
27+
28+
let test =
29+
url
30+
->processImage
31+
->Promise.thenResolve(event => {
32+
// similar to the above chain, but done all at once without interim variables
33+
let img =
34+
event
35+
->Event.currentTarget
36+
->EventTarget.unsafeAsElement
37+
->HtmlImageElement.ofElement
38+
->Belt.Option.getUnsafe
39+
let width = img->HtmlImageElement.naturalWidth
40+
let height = img->HtmlImageElement.naturalHeight
41+
Js.log4("width", width, "height", height)
42+
})

lib/js/examples/image_examples.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
var Curry = require("rescript/lib/js/curry.js");
4+
var Belt_Option = require("rescript/lib/js/belt_Option.js");
5+
var Caml_option = require("rescript/lib/js/caml_option.js");
6+
var Webapi__Dom__Document = require("../src/Webapi/Dom/Webapi__Dom__Document.js");
7+
var Webapi__Dom__HtmlImageElement = require("../src/Webapi/Dom/Webapi__Dom__HtmlImageElement.js");
8+
9+
var url = "";
10+
11+
function processImage(url) {
12+
return new Promise((function (resolve, reject) {
13+
var e = document.createElement("img");
14+
var maybeImg = Curry._1(Webapi__Dom__HtmlImageElement.ofElement, e);
15+
maybeImg.src = url;
16+
maybeImg.addEventListener("load", (function ($$event) {
17+
return resolve($$event);
18+
}));
19+
maybeImg.addEventListener("error", (function (error) {
20+
return reject(error);
21+
}));
22+
var body = Belt_Option.flatMap(Webapi__Dom__Document.asHtmlDocument(document), (function (prim) {
23+
return Caml_option.nullable_to_opt(prim.body);
24+
}));
25+
body.appendChild(maybeImg);
26+
body.removeChild(maybeImg);
27+
28+
}));
29+
}
30+
31+
var test = processImage(url).then(function ($$event) {
32+
var img = Curry._1(Webapi__Dom__HtmlImageElement.ofElement, $$event.currentTarget);
33+
var width = img.naturalWidth;
34+
var height = img.naturalHeight;
35+
console.log("width", width, "height", height);
36+
37+
});
38+
39+
exports.url = url;
40+
exports.processImage = processImage;
41+
exports.test = test;
42+
/* test Not a pure module */

0 commit comments

Comments
 (0)