Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
CC Attribution NonCommercial ShareAlike
Mouse Interaction Demo
Levin
xxxxxxxxxx
// Demonstration of cursor interaction techniques.
// This demonstrates how various aspects of mouse movement
// (such as speed, click frequency, proximity to a coordinate,
// or angle-with-respect-to a coordinate) can be used as
// elements of an interaction vocabulary.
var colorA;
var colorB;
var avgSpeed = 0;
var lastClickTime = 0;
var avgClickGap = 0;
function setup() {
createCanvas(600, 600);
colorA = color("yellow");
colorB = color("red");
}
//---------------------------------
function draw() {
background("DodgerBlue");
// An object that responds to how close the cursor is
drawProximityDesign();
// An object that responds to the cursor's orientation to it
drawOrientationDesign();
// An object that responds to the cursor's average speed
drawSpeedDesign();
// An object that responds to how frequently you click
drawFrequencyDesign();
}
//---------------------------------
function mousePressed(){
// Update the data used for calculating click frequency.
var clickGap = min((millis() - lastClickTime)/1000.0, 10);
avgClickGap = 0.8*avgClickGap + 0.2*clickGap;
lastClickTime = millis();
}
//---------------------------------
function drawProximityDesign(){
var thingX = 200;
var thingY = 200;
let dx = mouseX - thingX;
let dy = mouseY - thingY;
let proximity = sqrt(dx*dx + dy*dy);
// var proximity = dist(thingX, thingY, mouseX, mouseY);
var thingD = map(proximity, 0,200, 150,50);
thingD = constrain(thingD, 50,150);
var blendAmount = map(proximity, 0,200, 0,1);
var blendColor = lerpColor(colorA, colorB, blendAmount);
fill(blendColor);
strokeWeight(5);
stroke("black");
circle(thingX, thingY, thingD);
// Draw debug info.
noStroke();
fill("white");
textSize(18);
textAlign(CENTER);
text("Proximity: " + nf(proximity,1,1) + " px", thingX, thingY-100);
}
//---------------------------------
function drawOrientationDesign(){
var doodadX = 400;
var doodadY = 200;
var dy = mouseY - doodadY;
var dx = mouseX - doodadX;
var orientation = atan2(dy, dx);
var blendAmount = map(cos(orientation), -1,1, 0,1);
var blendColor = lerpColor(colorA, colorB, blendAmount);
fill(blendColor);
strokeWeight(5);
stroke("black");
rectMode(CENTER);
push();
translate(doodadX, doodadY);
rotate(orientation);
rect(0,0, 150, 50);
pop();
// Draw debug info.
noStroke();
fill("white");
textSize(18);
textAlign(CENTER);
var ang = degrees(orientation);
text("Orientation: " + nf(ang,1,1) +"°", doodadX, doodadY-100);
}
//---------------------------------
function drawSpeedDesign(){
// Compute a smoothed, running average of our speed.
var traveled = dist(mouseX, mouseY, pmouseX, pmouseY);
var A = 0.97;
var B = 1.0-A;
avgSpeed = A*avgSpeed + B*traveled;
var angle = map(avgSpeed, 0,150, 360, 0);
var blendAmount = map(avgSpeed, 0,150, 0,1);
var blendColor = lerpColor(colorA, colorB, blendAmount);
var boopX = 200;
var boopY = 400;
fill(blendColor);
stroke("black");
strokeWeight(5);
strokeJoin(ROUND);
arc(boopX, boopY, 150,150, 0, radians(angle), PIE);
// Draw debug info.
noStroke();
fill("white");
textSize(18);
textAlign(CENTER);
text("Avg.Speed: \n" + nf(avgSpeed,1,1) + " px/frame", boopX, boopY+120);
}
//---------------------------------
function drawFrequencyDesign(){
var bipX = 400;
var bipY = 400;
var clickFreq = 1.0/constrain(avgClickGap, 0.1, 10);
var myRectW = map(clickFreq, 0, 10, 50,150);
myRectW = constrain(myRectW, 50, 150);
var blendAmount = map(clickFreq, 0,10, 1,0);
var blendColor = lerpColor(colorA, colorB, blendAmount);
rectMode(CORNER);
stroke("black");
strokeWeight(5);
fill(blendColor);
rect(bipX-50, bipY-50, myRectW, 100);
// Draw debug info.
noStroke();
fill("white");
textSize(18);
textAlign(CENTER);
text("Click Frequency: \n" + nf(clickFreq,1,3) + "/sec", bipX, bipY+120);
}
See More Shortcuts
Please verify your email to comment
Verify Email