xxxxxxxxxx
184
// level 2: "poem level?" poem drift?
// drop word's of a poem, watch it land
// to do:
//"Do not run. Do
// not run. Do not
// run. Slowly
// go where you are going
// is yourself."
// --Juan Ramón Jiménez (tr. Kudinov)
// https://www.poetryfoundation.org/poems/51759/i-am-not-i
let Engine = Matter.Engine;
let World = Matter.World;
let Bodies = Matter.Bodies;
let Body = Matter.Body;
let Constraint = Matter.Constraint;
let Mouse = Matter.Mouse;
let MouseConstraint = Matter.MouseConstraint;
let engine;
let world;
let letters = [];
var circles = [];
var boundaries = [];
let ground;
let gravity;
let mConstraint;
let fontI;
let fontR;
let fontB;
const yAxis = 1
let prevX, prevY
let poemI = 0;
let poem = ["Do", "not", "run.", "Do", "not", "run.", "Do", "not", "run.", "Slowly", "go", "where", "you", "are", "going", "is", "yourself."];
function preload() {
fontR = loadFont('SpaceMono-Regular.ttf');
fontB = loadFont('SpaceMono-Bold.ttf');
fontI = loadFont('SpaceMono-BoldItalic.ttf');
}
function setup() {
var canvas = createCanvas(windowWidth, windowHeight);
engine = Engine.create();
world = engine.world;
// Matter.Bodies.rectangle(x, y, width, height, [options])
// ground
ground = Bodies.rectangle(windowWidth / 2, windowHeight, windowWidth, 50, {
isStatic: true
});
World.add(world, ground)
// ceiling
ground = Bodies.rectangle(windowWidth / 2, 0, windowWidth, 50, {
isStatic: true
});
World.add(world, ground)
// right
ground = Bodies.rectangle(windowWidth, 0, 50, windowHeight * 2, {
isStatic: true
});
World.add(world, ground)
// left
ground = Bodies.rectangle(0, 0, 50, windowHeight * 2, {
isStatic: true
});
World.add(world, ground)
gravity =
world.gravity.y = 0.5;
World.add(world, gravity)
}
function mousePressed() {
circles.push(new Circle(
mouseX,
mouseY,
this.w,
this.h,
poem[poemI]
))
poemI = (poemI + 1) % poem.length
}
function removeFromWorld() {
for (let circle of circles) {
World.remove(world, circle.body);
}
}
function keyPressed() {
if (keyCode === 32) {
removeFromWorld();
circles.splice(circles)
}
}
function draw() {
background(154, 218, 3)
Engine.update(engine)
textAlign(LEFT)
fill(255)
textFont(fontI)
textSize(15)
text("poem drift", 20, 40)
textFont(fontB)
textSize(10)
text("use your mouse to click and drop", 20, 60)
text("spacebar to reset", 20, 70)
text("poem by Juan Ramón Jiménez", 20, 90)
for (var i = 0; i < boundaries.length; i++) {
boundaries[i].show();
}
for (var i = 0; i < circles.length; i++) {
circles[i].show();
}
}
function Circle(x, y, w, h, poem) {
var options = {
friction: 0.8,
restitution: 0.4
}
this.ptSize = 40
this.poem = poem
let points = fontB.textToPoints(this.poem, 0, 0, this.ptSize)
let bounds = fontB.textBounds(this.poem, 0, 0, this.ptSize)
this.w = bounds.w
this.h = bounds.h
for (let pt of points) {
pt.x = pt.x - bounds.x - bounds.w / 2
pt.y = pt.y - bounds.y - bounds.h / 2
}
this.body = Bodies.rectangle(x, y, bounds.w, bounds.h, options)
this.poem = poem
World.add(world, this.body)
this.show = function() {
var pos = this.body.position
var angle = this.body.angle
push()
translate(pos.x, pos.y)
rotate(angle)
rectMode(CENTER)
noStroke()
//fill(0)
fill(254, 8, 246)
noStroke()
textFont(fontB)
textSize(40)
textAlign(CENTER, CENTER)
translate(0, 0)
text(this.poem, 0, 0)
pop()
}
}