@@ -39,23 +39,32 @@ Most names have been shortened from names of the raw browser APIs, since the nam
3939
4040## Examples
4141
42- ### Appending a child to a ` Node `
42+ You can start using the bindings using the following import:
4343
44- ``` scala mdoc:js
44+ ``` scala mdoc:js:shared
4545import org .scalajs .dom ._
46+ ```
47+
48+
49+ ### Appending a child to a ` Node `
4650
51+
52+ ``` scala mdoc:js:shared
4753def appendElement (div : html.Div ): Unit = {
4854 val child = document.createElement(" div" )
4955 child.textContent = " I can add elements to DOM elements!"
5056 div.appendChild(child)
5157}
5258```
5359
60+ ``` scala mdoc:js:invisible
61+ <button class =" button-run" >Run </ button
62+ ---
63+ ```
64+
5465### Add an EventListener for ` onmousemove `
5566
5667``` scala mdoc:js
57- import org .scalajs .dom ._
58-
5968def showOnMouseCoordinates (pre : html.Pre ): Unit = {
6069 pre.onmousemove = (ev : MouseEvent ) =>
6170 pre.textContent = s """
@@ -73,23 +82,22 @@ def showOnMouseCoordinates(pre: html.Pre): Unit = {
7382``` scala mdoc:js
7483def storeInputInLocalStorage (input : html.Input , box : html.Div ) = {
7584 val key = " myKey"
76- input.value = dom. window.localStorage.getItem(key)
85+ input.value = window.localStorage.getItem(key)
7786
78- input.onkeyup = { (e : dom. Event ) =>
79- dom. window.localStorage.setItem(
87+ input.onkeyup = { (e : Event ) =>
88+ window.localStorage.setItem(
8089 key, input.value
8190 )
8291
83- output .textContent = s " Saved: ${input.value} to local storage! "
92+ box .textContent = s " Saved: ${input.value} to local storage! "
8493 }
8594}
8695```
8796
8897### Using ` Canvas ` to draw
8998
9099``` scala mdoc:js
91-
92- type Context2D = dom.CanvasRenderingContext2D
100+ type Context2D = CanvasRenderingContext2D
93101
94102def drawCuteSmiley (canvas : html.Canvas ) = {
95103 val context = canvas.getContext(" 2d" ).asInstanceOf [Context2D ]
@@ -115,41 +123,47 @@ def drawCuteSmiley(canvas: html.Canvas) = {
115123### Using ` Fetch ` to make API calls in the browser
116124
117125``` scala mdoc:js
126+ import scala .concurrent .ExecutionContext .Implicits .global
127+
118128def fetchBoredApi (element : html.Pre ) = {
119129 val url =
120130 " https://www.boredapi.com/api/activity"
121131
122132 val responseText = for {
123- response <- dom. fetch(url)
124- text <- response.text()
133+ response <- fetch(url).toFuture
134+ text <- response.text().toFuture
125135 } yield {
126136 text
127137 }
128138
129139 for (text <- responseText)
130- pre .textContent = text
140+ element .textContent = text
131141}
132142```
133143
144+
145+
134146### Using Websockets
135147
136- ``` scala mdoc:js
137- def echoWebSocket (input : html.Input , pre : html.Pre ) = {
138- val echo = " wss://echo.websocket.org"
139- val socket = new dom.WebSocket (echo)
140-
141- socket.onmessage = {
142- (e : dom.MessageEvent ) =>
143- pre.textContent +=
144- e.data.toString
145- }
146148
147- socket.onopen = { (e : dom.Event ) =>
148- in.onkeyup = { (e : dom.Event ) =>
149- socket.send(input.value)
150- }
151- }
152- }
149+ ``` scala mdoc:js
150+ // TODO: currently crashes with an error
151+ // def echoWebSocket(input: html.Input, pre: html.Pre) = {
152+ // val echo = "wss://echo.websocket.org"
153+ // val socket = new WebSocket(echo)
154+
155+ // socket.onmessage = {
156+ // (e: MessageEvent) =>
157+ // pre.textContent +=
158+ // e.data.toString
159+ // }
160+
161+ // socket.onopen = { (e: Event) =>
162+ // input.onkeyup = { (e: Event) =>
163+ // socket.send(input.value)
164+ // }
165+ // }
166+ // }
153167```
154168
155169### Styling an HTML element
0 commit comments