Published
Edited
Sep 7, 2020
4 forks
16 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Randomly pick numbers between 0 and 1
random_values = _.range(10000).map(d => Math.random())
Insert cell
Insert cell
Insert cell
Insert cell
// Require a library for generating simplex noise
SimplexNoise = require('simplex-noise@2.4')
Insert cell
// Create a new instance of our simplex noise object
simplex = new SimplexNoise()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
svg`<svg width="${w}" height="${h}">
<rect x="0" y="0" width="${w}" height="${h}" stroke="none" fill="black"></rect>
<g>
${_.range(num_rects)
.map(i => {
const angle = (Math.PI * 2 * i) / num_rects;
const mag = w / 4 + 70;
const size = 50;
const pos = PVector((w - size) / 2, (h - size) / 2).add(
PVector()
.setMag(mag)
.rotateBy(angle)
);

return `<rect
width="${size}"
height="${size}"
stroke="white"
fill="none"
transform="translate(${pos.x},${pos.y})"
/>`;
})
.join('\n')}
</g>
</svg>`
Insert cell
Insert cell
svg`<svg width="${w}" height="${h}">
<rect x="0" y="0" width="${w}" height="${h}" stroke="none" fill="black"></rect>
<g>
${_.range(num_rects)
.map(i => {
const angle = (Math.PI * 2 * i) / num_rects;
const mag = w / 4 + 70;
const pos = PVector((w - 50) / 2, (h - 50) / 2).add(
PVector()
.setMag(mag)
.rotateBy(angle)
);

const size = 75 * simplex.noise2D(pos.x / 15, pos.y / 15);

return `<rect
width="${size}"
height="${size}"
stroke="white"
fill="none"
transform="translate(${pos.x},${pos.y})"
/>`;
})
.join('\n')}
</g>
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Yield a value, 60 times a second
iterator = {
let i = 0;
while (true) {
yield ++i;
}
}
Insert cell
Insert cell
// Slow it down a bit!
slow_iterator = {
let i = 0;
while (true) {
yield Promises.delay(50, ++i);
}
}
Insert cell
Insert cell
// We'll use this to loop animations
loop = {
while (true) {
yield* _.range(-100, 100);
}
}
Insert cell
// We'll use this to loop animations a bit more slowly
slow_loop = {
let i = 0;
let direction = "up";
while (true) {
await Promises.delay(10);
if (i === 100) {
direction = "down";
} else if (i === -100) {
direction = "up";
}
i = direction === "up" ? i + 1 : i - 1;
yield i;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
roses = await FileAttachment("roses.jpg").url()
Insert cell
html`<img id="roses" src=${roses} />`
Insert cell
img_points = {
// Render as canvas to sample the colors
const img = document.querySelector("#roses");
img.src = roses;
img.crossOrigin = "Anonymous";
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
return svg`<svg height=${img.height} width=${width}>${_.range(1000).map(d => {
const x = Math.random() * width;
const y = Math.random() * img.height;
const rgb_color = canvas.getContext('2d').getImageData(x, y, 1, 1).data;
const color = `rgb(${rgb_color[0]},${rgb_color[1]},${rgb_color[2]})`;
return `<circle cx=${x} cy=${y} fill=${color} r=10 />`;
})}
</svg>`;
}
Insert cell
Insert cell
animated_img_points = {
// Render as canvas to sample the colors
const img = document.querySelector("#roses");
img.src = roses;
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const max_radius = 50;
const min_radius = 2;
canvas.getContext('2d').drawImage(img, 0, 0, canvas.width, canvas.height);
return svg`<svg height=${img.height} width=${width}>${positions.map(
(d, i) => {
const r = Math.abs(slow_loop - i) % 100;
const rgb_color = canvas.getContext('2d').getImageData(d.x, d.y, 1, 1)
.data;
const color = `rgb(${rgb_color[0]},${rgb_color[1]},${rgb_color[2]})`;
return `<circle cx=${d.x} cy=${d.y} fill=${color} r=${r} />`;
}
)}
</svg>`;
}
Insert cell
// Positions for the above points
positions = {
const img = document.querySelector("#roses");
return _.range(100).map(d => ({
x: Math.random() * width,
y: Math.random() * img.height
}));
}
Insert cell
Insert cell
_ = require("lodash")
Insert cell
// Assigning a label to use with the imported histogram function
rv = Object.assign(random_values, {
y: "Number of Observations"
})
Insert cell
import { slider } from "@jashkenas/inputs"
Insert cell
import { chart as histogram } with { rv as data } from "@d3/histogram"
Insert cell
import { code_styles, displayCaution } from "@info474/utilities"
Insert cell
PVector = require('https://bundle.run/pvectorjs@1.2.0')
Insert cell
w = 500
Insert cell
num_rects = 200
Insert cell
h = 500
Insert cell
code_styles
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.
Learn more