Platform
Resources
Pricing
Sign in
Get started
Kevin Kwok
Workspace
Fork
Published
By
Kevin Kwok
Edited
Mar 26, 2020
Insert cell
Insert cell
Insert cell
2
+
2
Insert cell
{
const
canvas
=
DOM
.
canvas
(
width
,
33
)
;
const
context
=
canvas
.
getContext
(
"2d"
)
;
context
.
fillText
(
"Hello, I am a canvas!"
,
0
,
20
)
;
return
canvas
;
}
Insert cell
{
const
context
=
DOM
.
context2d
(
width
,
33
)
;
context
.
fillText
(
"Hello, I am a canvas!"
,
0
,
20
)
;
return
context
.
canvas
;
}
Insert cell
DOM
.
download
(
new
Blob
(
[
JSON
.
stringify
(
{
hello
:
"world"
}
)
]
,
{
type
:
"application/json"
}
)
,
"hello.json"
,
// optional file name
"Click to Download"
// optional button label
)
Insert cell
{
// Consider using the html tagged template literal instead.
const
i
=
DOM
.
element
(
"i"
)
;
i
.
textContent
=
"Hello, world!"
;
return
i
;
}
Insert cell
{
// Consider using DOM.svg or the html tagged template literal instead.
const
svg
=
DOM
.
element
(
"svg:svg"
,
{
width
,
height
:
27
}
)
;
const
text
=
svg
.
appendChild
(
DOM
.
element
(
"svg:text"
,
{
y
:
22
}
)
)
;
text
.
textContent
=
"Hello, I am SVG!"
;
return
svg
;
}
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
input
(
)
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
input
(
"checkbox"
)
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
input
(
"file"
)
Insert cell
// Consider using DOM.range or the html tagged template literal instead.
DOM
.
input
(
"range"
)
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
range
(
0
,
// min
10
,
// max
1
// step
)
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
select
(
[
"one"
,
"two"
,
"three"
]
)
Insert cell
{
// Consider using the html tagged template literal instead.
const
svg
=
DOM
.
svg
(
width
,
27
)
;
const
text
=
svg
.
appendChild
(
DOM
.
element
(
"svg:text"
,
{
y
:
22
}
)
)
;
text
.
textContent
=
"Hello, I am SVG!"
;
return
svg
;
}
Insert cell
// Consider using the html tagged template literal instead.
DOM
.
text
(
"I am a text node."
)
Insert cell
Insert cell
html
`<span style="background:yellow;">
Hello, <i>world</i>!
</span>`
Insert cell
html
`<input type=range min=0 max=10 step=1>`
Insert cell
html
`<select>
<option>one</option>
<option>two</option>
<option>three</option>
</select>`
Insert cell
md
`Hello, *world*!`
Insert cell
md
`The current time is ${
new
Date
(
now
)
}.`
// Reactive Markdown!
Insert cell
tex
`E = mc^2`
Insert cell
tex
`
f(x) = \int_{-\infty}^\infty
\hat f(\xi)\,e^{2 \pi i \xi x}
\,d\xi
`
Insert cell
tex
.
block
`
f(x) = \int_{-\infty}^\infty
\hat f(\xi)\,e^{2 \pi i \xi x}
\,d\xi
`
Insert cell
Insert cell
Files
// Expand me!
Insert cell
viewof
file
=
DOM
.
input
(
"file"
)
// Try opening a file.
Insert cell
Files
.
buffer
(
file
)
Insert cell
Files
.
text
(
file
)
Insert cell
Files
.
url
(
file
)
Insert cell
Insert cell
Generators
// Expand me!
Insert cell
function
*
integers
(
)
{
let
i
=
-
1
;
while
(
true
)
{
yield
++
i
;
}
}
Insert cell
Generators
.
filter
(
integers
(
)
,
d
=>
d
&
1
)
// Odd integers.
Insert cell
inputElement
=
DOM
.
range
(
)
Insert cell
// Consider using viewof instead.
inputValue
=
Generators
.
input
(
inputElement
)
Insert cell
// viewof uses Generators.input (and Generators.observe) internally.
viewof
value
=
DOM
.
range
(
)
Insert cell
value
Insert cell
Generators
.
map
(
integers
(
)
,
d
=>
d
**
2
)
// Squares.
Insert cell
// Consider using Generators.input or viewof instead.
// Like Generators.queue, except it yields only the latest notified value.
Generators
.
observe
(
notify
=>
{
const
mousemoved
=
event
=>
notify
(
[
event
.
clientX
,
event
.
clientY
]
)
;
window
.
addEventListener
(
"mousemove"
,
mousemoved
)
;
return
(
)
=>
window
.
removeEventListener
(
"mousemove"
,
mousemoved
)
;
}
)
Insert cell
// Consider using Generators.input or viewof instead.
// Like Generators.observe, except it yields all notified values.
Generators
.
queue
(
notify
=>
{
const
mousemoved
=
event
=>
notify
(
[
event
.
clientX
,
event
.
clientY
]
)
;
window
.
addEventListener
(
"mousemove"
,
mousemoved
)
;
return
(
)
=>
window
.
removeEventListener
(
"mousemove"
,
mousemoved
)
;
}
)
Insert cell
Generators
.
valueAt
(
integers
(
)
,
10
)
// Returns the 11th yield value (zero-based index).
Insert cell
Insert cell
Promises
// Expand me!
Insert cell
// Consider also Promises.tick.
Promises
.
delay
(
5000
,
"resolved value"
)
Insert cell
invalidation
// It never resolves!
Insert cell
{
let
i
=
0
;
const
div
=
html
`<div>`
;
const
timer
=
d3
.
timer
(
(
)
=>
div
.
textContent
=
++
i
)
;
try
{
yield
div
;
yield
invalidation
;
// Let the timer run forever,
}
finally
{
timer
.
stop
(
)
;
// but dispose if re-evaluated.
}
}
Insert cell
// Like Promises.delay, but synchronized.
Promises
.
tick
(
1000
,
"resolved value"
)
Insert cell
{
while
(
true
)
{
yield
Promises
.
tick
(
1000
)
.
then
(
(
)
=>
new
Date
)
;
}
}
Insert cell
{
// A somewhat elaborate variation of Promises.delay.
const
date
=
new
Date
;
date
.
setSeconds
(
date
.
getSeconds
(
)
+
1
)
;
return
Promises
.
when
(
date
,
"resolved value"
)
;
}
Insert cell
Insert cell
d3
=
require
(
"d3@4"
)
Insert cell
topojson
=
require
(
"topojson-client"
,
"topojson-simplify"
)
// Merge libraries!
Insert cell
require
(
"https://unpkg.com/d3@4/build/d3.js"
)
// URLs work, too.
Insert cell
resolve
(
"d3@4"
)
Insert cell
Insert cell
now
// Date.now()
Insert cell
width
// Try resizing the window.
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
file
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
integers
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
inputElement
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
inputValue
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
value
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
d3
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
topojson
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML