@@ -39,23 +39,32 @@ Most names have been shortened from names of the raw browser APIs, since the nam
39
39
40
40
## Examples
41
41
42
- ### Appending a child to a ` Node `
42
+ You can start using the bindings using the following import:
43
43
44
- ``` scala mdoc:js
44
+ ``` scala mdoc:js:shared
45
45
import org .scalajs .dom ._
46
+ ```
47
+
48
+
49
+ ### Appending a child to a ` Node `
46
50
51
+
52
+ ``` scala mdoc:js:shared
47
53
def appendElement (div : html.Div ): Unit = {
48
54
val child = document.createElement(" div" )
49
55
child.textContent = " I can add elements to DOM elements!"
50
56
div.appendChild(child)
51
57
}
52
58
```
53
59
60
+ ``` scala mdoc:js:invisible
61
+ <button class =" button-run" >Run </ button
62
+ ---
63
+ ```
64
+
54
65
### Add an EventListener for ` onmousemove `
55
66
56
67
``` scala mdoc:js
57
- import org .scalajs .dom ._
58
-
59
68
def showOnMouseCoordinates (pre : html.Pre ): Unit = {
60
69
pre.onmousemove = (ev : MouseEvent ) =>
61
70
pre.textContent = s """
@@ -73,23 +82,22 @@ def showOnMouseCoordinates(pre: html.Pre): Unit = {
73
82
``` scala mdoc:js
74
83
def storeInputInLocalStorage (input : html.Input , box : html.Div ) = {
75
84
val key = " myKey"
76
- input.value = dom. window.localStorage.getItem(key)
85
+ input.value = window.localStorage.getItem(key)
77
86
78
- input.onkeyup = { (e : dom. Event ) =>
79
- dom. window.localStorage.setItem(
87
+ input.onkeyup = { (e : Event ) =>
88
+ window.localStorage.setItem(
80
89
key, input.value
81
90
)
82
91
83
- output .textContent = s " Saved: ${input.value} to local storage! "
92
+ box .textContent = s " Saved: ${input.value} to local storage! "
84
93
}
85
94
}
86
95
```
87
96
88
97
### Using ` Canvas ` to draw
89
98
90
99
``` scala mdoc:js
91
-
92
- type Context2D = dom.CanvasRenderingContext2D
100
+ type Context2D = CanvasRenderingContext2D
93
101
94
102
def drawCuteSmiley (canvas : html.Canvas ) = {
95
103
val context = canvas.getContext(" 2d" ).asInstanceOf [Context2D ]
@@ -115,41 +123,47 @@ def drawCuteSmiley(canvas: html.Canvas) = {
115
123
### Using ` Fetch ` to make API calls in the browser
116
124
117
125
``` scala mdoc:js
126
+ import scala .concurrent .ExecutionContext .Implicits .global
127
+
118
128
def fetchBoredApi (element : html.Pre ) = {
119
129
val url =
120
130
" https://www.boredapi.com/api/activity"
121
131
122
132
val responseText = for {
123
- response <- dom. fetch(url)
124
- text <- response.text()
133
+ response <- fetch(url).toFuture
134
+ text <- response.text().toFuture
125
135
} yield {
126
136
text
127
137
}
128
138
129
139
for (text <- responseText)
130
- pre .textContent = text
140
+ element .textContent = text
131
141
}
132
142
```
133
143
144
+
145
+
134
146
### Using Websockets
135
147
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
- }
146
148
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
+ // }
153
167
```
154
168
155
169
### Styling an HTML element
0 commit comments