Skip to content

Instantly share code, notes, and snippets.

@GoSubRoutine
Last active November 8, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoSubRoutine/22e47de702d3c8d86585f80f652d8179 to your computer and use it in GitHub Desktop.
Save GoSubRoutine/22e47de702d3c8d86585f80f652d8179 to your computer and use it in GitHub Desktop.
Image Drop
height: 600
scrolling: no
border: yes
<script async src=https://cdn.JsDelivr.net/npm/p5></script>
<script defer src=sketch.js></script>
/**
* Image Drop (v3.0.4)
* GoToLoop (2017-Oct-26) (2015-Nov-25)
*
* https://Forum.Processing.org/two/discussion/24750/
* p5js-drag-n-drop-hover-not-working-why#Item_3
*
* https://Forum.Processing.org/two/discussion/13650/
* dropped-image-not-showing-in-the-draw-loop-gotfile-createimg-image#Item_4
*
* https://Bl.ocks.org/GoSubRoutine/22e47de702d3c8d86585f80f652d8179
* http://p5js.SketchPad.cc/sp/pad/view/ro.CYTkHWj9smgw8Q/latest
*/
"use strict";
let img, bg, hover;
function setup() {
createCanvas(800, 600).drop(gotFile).dragOver(highlight).dragLeave(redraw);
textAlign(CENTER).textSize(32).textStyle(BOLD).noLoop();
colorMode(RGB).imageMode(CORNER);
fill('yellow').noStroke();
bg = color(0o200);
hover = color('red');
}
function draw() {
background(bg);
if (img) image(img, 0, 0, width, height);
else text('Drag an image file onto the canvas.', width>>1, height>>1);
}
function gotFile(f) {
print(`File "${f.name}" was dropped into sketch's canvas.`);
if (f.type === 'image') return img = loadImage(f.data, redraw, console.error);
console.warn(`However, type "${f.type}" isn't an image!`);
redraw();
}
function highlight(evt) {
this.background(hover);
evt.preventDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment