let days
let months
let years
function setup() {
// Our anniversary is 4/7/2019
createCanvas(600, 600);
days = day()-7
months = month()-4
years = year()
}
function draw() {
background(255, 0, 150);
//heart
strokeWeight(0)
fill(255, 0, 0)
ellipse(200, 200, 250, 250)
ellipse(400, 200, 250, 250)
triangle(110, 290, 300, 490, 490, 290)
ellipse(300, 300, 100, 100)
//text
fill(0)
textStyle(BOLD);
textSize(20)
textAlign(CENTER, TOP)
text('It has been ' + months + ' months and ' + days + ' days since \n we first started dating', 300, 250)
}
My problem is that after the new year or any month change the dates will go into the negatives.
How would I make it so it keeps track of today's date and the set date and tells me the time in between them?
I'll point you in the right direction.
JavaScript has a Date object you can use:
let anniversary;
function setup() {
// Our anniversary is 4/7/2019
createCanvas(600, 600);
anniversary = new Date(2019, 7, 4); // initialise your anniversary
You can also get the current date by simply newing up a new Date()
let currentDate = new Date();
You could use this to calculate the difference between the two dates, there's endless resources for this. Here's one to get you going!
Related
I am trying to move a car I drew using Processing. For drawing this Car I have used multiple methods like rect, line, circle, and I want to move this car along the x axis, back and forth. But when I try to move it by increasing the X- axis of all the figures, then it makes different figure, I wanted to know how can I move this car as a one object.Picture of the car
float circle1X = 120;
float circle1Y = 250;
float circle2X = 370;
float circle2Y = 250;
void setup(){
size(1200, 600);
background(135,206,235);
}
void draw(){
noFill();
rect(50, 200, 390, 50);
line(150, 200, 200, 140);
line(200, 140, 360, 140);
line(360, 140, 380, 200);
line(165, 200, 205, 150);
line(205, 150, 350, 150);
line(350, 150, 368, 200);
line(165, 200, 165, 250);
circle(circle1X, circle1Y, 70);
circle(circle1X, circle1Y, 55);
circle(circle2X, circle2Y, 70);
circle(circle2X, circle2Y, 55);
}
The idea to grasp here is to love matrix stacks and transformations (like translate).
Remember how your drawing's coordinates origin on the top left corner of the screen? That's your current coordinate system's origin point. When applying transformations, you decide on a transformation and you apply it to the whole coordinate system. As an example, if I write this:
rect(100, 100, 150, 150);
translate(300, 100);
rect(100, 100, 150, 150);
I'll get two different rectangles even though the coordinates I wrote were the same, because I modified where the origin point of my coordinate system is before drawing the second rectangle:
So you already get the idea that a simple translation would move your car around just fine. But that wouldn't work if you have, say, 2 cars to move around in the same sketch: transformations are cumulative. Of course, you can cancel one translate with his exact opposite, but it would feel stupid, wouldn't it? And what if you made some complicated mix of translations, rotations and whatever else?
We're lucky, though! The good people at the Processing Foundation though about it and added the pushMatrix() and the popMatrix() commands. The pushMatrix() method "save" your coordinate system just like it currently is. Then you can do a couple transformation, draw stuff and then "pop" the matrix and go back to your original coordinate system. I'm doing it with your car in this example:
float circle1X = 120;
float circle1Y = 250;
float circle2X = 370;
float circle2Y = 250;
void setup() {
size(1200, 600);
}
void draw() {
// background() goes here or you'll never "clean" the screen (you'll draw every frame over the last one)
background(135, 206, 235);
drawCar(); // car #1 to see where the coordinates are drawing the car
pushMatrix();
translate(500, 0); // I'm moving everything I'll draw next 500 pixels right
drawCar(); // car #2 is 500 pixels right
popMatrix();
rect(100, 100, 200, 200); // this random rectangle will appear exactly where it should (the translation had no effect since it was popped before)
}
// I moved these lines in their own method, it's easier to read and modify this way
void drawCar() {
noFill();
rect(50, 200, 390, 50);
line(150, 200, 200, 140);
line(200, 140, 360, 140);
line(360, 140, 380, 200);
line(165, 200, 205, 150);
line(205, 150, 350, 150);
line(350, 150, 368, 200);
line(165, 200, 165, 250);
circle(circle1X, circle1Y, 70);
circle(circle1X, circle1Y, 55);
circle(circle2X, circle2Y, 70);
circle(circle2X, circle2Y, 55);
}
As you can see, it works flawlessly and it's fairly easy to grasp and use. In fact, it can be waaay easier to draw with matrixes: instead of drawing your car "where you want to see it", you can draw it around the (0, 0) point of your sketch, and use the (0, 0) point as the anchor of your car drawing, making it's coordinates a lot easier to manage (sorry I messed the car's drawing a little bit but you'll get the idea anyway): in this modified sketch, I use a carX integer as the X coordinate of the car, and the car's "anchor point" is the top left corner of the car's drawing (I tried to substract the unneeded X and Y slack from the car and ended up messing it a little bit). Every time you mouse click, the car moves right a little because the translation uses the incremented variable:
float circle1X = 70;
float circle1Y = 200;
float circle2X = 320;
float circle2Y = 200;
int carX = 0;
void setup() {
size(1200, 600);
}
void draw() {
background(135, 206, 235);
pushMatrix();
translate(carX, 0);
drawCar();
popMatrix();
}
void mouseClicked() {
carX += 50;
}
void drawCar() {
noFill();
rect(0, 150, 390, 0);
line(100, 150, 150, 90);
line(150, 90, 310, 90);
line(310, 90, 330, 150);
line(115, 150, 155, 100);
line(155, 100, 300, 100);
line(300, 100, 318, 150);
line(115, 150, 115, 200);
circle(circle1X, circle1Y, 70);
circle(circle1X, circle1Y, 55);
circle(circle2X, circle2Y, 70);
circle(circle2X, circle2Y, 55);
}
The idea here is that the "anchor point" is close to the drawing, and the car's coordinates are use to translate it to where it should be drawn. More elegant, less complicated, easier to use.
I told you we could draw 2 different cars easily, right? Here's a quick and dirty example (modify the draw() loop only):
int carDelta = 0;
void draw() {
background(135, 206, 235);
// car #1 is going right
pushMatrix();
translate(carDelta, 0);
drawCar();
popMatrix();
// car #2 is going down
pushMatrix();
translate(0, carDelta);
drawCar();
popMatrix();
}
Hope this helps. Have fun!
Quick and dirty solution:
You can translate the anchor point from where everything is drawn with.
This way you dont have do change anything from your drawing.
However this doesnt really help you in the long way.
// at the start
in x1 = 0;
// in draw before you draw your car
x1++;
translate(x1, 0);
Nicer solution:
Learn about classes and try recreating the car as such.
Include a variable for its position.
https://processing.org/reference/class.html
And check out the tutorials by Daniel Shiffman
https://www.youtube.com/watch?v=lmgcMPRa1qw
I am trying to make a button appear after a video has played. I know this has got to do with setInterval, but I'm unsure how to go about implementing this into my project. the video is roughly 40 seconds long, so I would like a way to show the button after the video has finished playing. I am using the p5.js framework.
it is meant to be a loading screen for a game that I am working on, after the video has finished a button should appear to let you advance to start the game.
bellow is my code.
let bg;
let logo;
let button;
let vid;
let click;
function preload() {
bg = loadImage('assets/space.png');
logo = loadImage('assets/logo.png');
rock1 = loadImage('assets/rock1.png');
rock2 = loadImage('assets/rock2.png');
go = loadImage('assets/go.png');
hs = loadImage('assets/hs.png');
bg1 = loadImage('assets/space.png');
w = loadImage('assets/w.png');
a = loadImage('assets/a.png');
s = loadImage('assets/s.png');
d = loadImage('assets/d.png');
mouse = loadImage('assets/mouse.png');
click = loadSound('assets/click.mp3');
}
function setup() {
vid = createVideo('assets/vid.mp4')
bg = loadImage('assets/space1.png');
createCanvas(windowWidth, windowHeight);
strokeWeight(10);
// frameRate(30);
button = createButton('CLICK TO START');
button.position(650, 650);
button.mousePressed(soundPlay)
vid.loop();
}
function soundPlay() {
if (!click.isPlaying()) {
click.play();
}
}
function draw() {
background(bg);
stroke(189, 51, 164);
rect(190, 90, 1070, 600, 10);
for (let i = 200; i < 1250; i++) {
let r = random(255);
stroke(r);
line(i, 100, i, 680);
}
image(logo, 180, 70, 200, 200);
stroke(189, 51, 164);
line(740, 30, 700, 85);
line(660, 30, 700, 85);
circle(660, 30, 20);
circle(740, 30, 20);
image(rock1, 1300, 70, 100, 100);
image(rock2, 60, 70, 100, 100);
rect(400, 150, 650, 300, 10);
image(bg1, 400, 155, 650, 290);
rect(400, 450, 650, 180, 10);
image(bg1, 400, 455, 650, 170);
image(w, 410, 500, 25, 25);
image(a, 410, 530, 24, 24);
image(s, 410, 560, 24, 24);
image(d, 410, 590, 24, 24);
image(mouse, 710, 500, 25, 25);
noStroke();
fill(255)
text("CONTROLLS:", 660, 470);
textSize(20);
textStyle(BOLD);
text("SPACEBAR:", 710, 550);
image(vid, 400, 150, 650, 300);
text("LOADING: . . .", 650, 150);
}
You can try using vid.onended() to call a function when the video is done, to later display a button. documentation
As you're not using the video as its own element, you're plonking it in the canvas and drawing an image each frame -
you could simply wrap the image(vid, 400, 150, 650, 300); in an if statement:
if (showVid) {
image(vid, 400, 150, 650, 300);
}
And then you'll set the showVid variable to false when your video duration has ended:
const videoLength = 5000; // unsure if there's a way in p5.js to get a video's duration
setTimeout(() => showVid = false, videoLength)
Here's a really simple example I've put together, and a link to the p5.js sketch so you can play it.
let vid;
let showVid = true;
function setup() {
createCanvas(400, 400);
vid = createVideo('vid.mp4');
console.log(vid);
vid.hide();
vid.loop();
const videoLength = 5000; // unsure if there's a way in p5.js to get a video's duration
setTimeout(() => showVid = false, videoLength)
}
function draw() {
background(150);
if (showVid) {
image(vid, 0, 100);
}
}
I have the below code which moves a group of rectangles and circles, but for some reason it always keeps the previous shapes. I want to delete the previous shapes and only draw the new shapes (so it looks like my shapes are moving). I end up with this:
But it should look like a truck is moving. Below is my code:
public class Truck extends Vehicle {
public Truck(float size, float speed) {
super(size, speed);
}
int x = width-250;
void display() {
while (x > -100) {
scale(size);
translate(-1, 0);
fill(255, 0, 0);
rect(x, 500, 200, 100); //body
rect(x-75, 525, 75, 75); //front
fill(0);
rect(x-75, 525, 45, 45); //window
ellipse(x-60, 610, 45, 45);
ellipse(x-20, 605, 35, 35);
ellipse(x+160, 610, 45, 45);
ellipse(x+117, 605, 35, 35);
delay(1);
x--;
}
}
}
So I have quite a bit of experience with the JavaScript version of processing, and if you add:
background(255);
before you draw everything I think that will fix your issue.
This is because the shapes leave a "trail" of sorts behind them when they move. Unless you draw the background periodically, that trail will be visible.
When using Xamarin, what is the best or most accepted method of handling changes in orientation?
I have a carousel page which rotates through 5 content pages (the same page, just different text/images), in the content pages I have a 6 row grid, each row containing either an image, text or a button.
When changing from portrait to landscape I reset the size and padding of these within OnSizeAllocated. This seems to work randomly for 3 out of 5 content pages, the text in the other 2 will be out of position, and it's not always the same 3 that work either.
I'm guessing there's a better way to do this than manually resize things?
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (this.width != width || this.height != height)
{
this.width = width;
this.height = height;
//reconfigure layout
if (width > height)
{
img.HeightRequest = 62.5;
img.WidthRequest = 62.5;
logo.HeightRequest = 52.5;
logo.WidthRequest = 142.27;
headerStack.Padding = new Thickness(0, -60, 0, 0);
txtStack.Padding = new Thickness(20, -60, 20, 0);
sIndicator.Padding = new Thickness(20, 0, 20, 100);
sButton.Padding = new Thickness(0, -40, 0, 0);
}
else
{
img.WidthRequest = 250;
img.HeightRequest = 250;
logo.HeightRequest = 70;
logo.WidthRequest = 189.7;
headerStack.Padding = new Thickness(20, 40, 20, 0);
txtStack.Padding = new Thickness(20, 0, 20, 0);
sIndicator.Padding = new Thickness(20, 25, 20, 0);
}
}
}
With a few tweaks to what I was setting I got this working just fine, would still be interested in other peoples' views on how they do it.
Want to make a plot with the average values of HR and avg. Looked it up in the documentation, but it seems i cannot get the average command to work :(
Can anybody spot my stupid mistake ?
var svg = dimple.newSvg("#chartContainer", 590, 400);
d3.tsv("bbd3.tsv", function (data) {
var myChart = new dimple.chart(svg, data);
myChart.setBounds(75, 30, 490, 330)
myChart.addMeasureAxis("x", "HR");
myChart.addMeasureAxis("y", "avg");
myChart.aggregate = dimple.aggregateMethod.avg;
myChart.addSeries( ["handedness"], dimple.plot.bubble);
myChart.addLegend(180, 10, 360, 20, "right");
myChart.draw();
Aggregation applies to the series not the chart. So you can rewrite like this:
var mySeries = myChart.addSeries( ["handedness"], dimple.plot.bubble);
mySeries.aggregate = dimple.aggregateMethod.avg;