xxxxxxxxxx
130
//setImage
//setPosition
//setPenTipPosition
//moveTo
let drawerImage, drawerSound
p5.prototype.initDrawer = function(image='holding.png', sound='short_pencil_stroke.mp3'){
drawerSound = this.loadSound(sound)
drawerImage = this.loadImage(image)
}
p5.prototype.registerMethod('init', p5.prototype.initDrawer);
function Drawer(p) {
this.sketch = p || window;
this.setSpeed = function(speed) {
this.speed = speed
}
this.sound = drawerSound
this.setSound = function(p5AudioObject) {
this.sound = p5AudioObject
}
this.speed = 2;
this.canSaveSteps = false
this.steps = []
this.saveSteps = function() {
this.canSaveSteps = true
}
this.stopSaveSteps = function() {
this.canSaveSteps = false
this.steps = []
}
this.image = drawerImage;
this.setImage = function(p5ImageObject) {
this.image = p5ImageObject
}
this.position = this.sketch.createVector(this.sketch.width/2,this.sketch.height/2) ;
this.setPosition = function(vector) {
this.position = vector
}
this.targetPosition = undefined;
this.setTargetPosition = function(vector) {
this.targetPosition = vector
}
this.moveTo = function(vector, replaceSpeed = this.speed) {
this.setTargetPosition(vector)
this.startMovement(replaceSpeed)
}
this.getTargetDistance = function() {
return this.sketch.dist(this.targetPosition.x, this.targetPosition.y, this.position.x, this.position.y);
}
this.hasToMove = function() {
return this.targetPosition && this.position && this.getTargetDistance() > 0
}
this.startMovement = function(replaceSpeed) {
if (this.hasToMove())
this.updateToNextPosition(replaceSpeed)
this.draw()
this.playAudio()
}
this.playAudio = function(){
if(this.hasToMove() && !this.sound.isPlaying())
this.sound.loop();
else
this.sound.stop();
}
this.penTipPosition = this.sketch.createVector(-52, -128)
this.setPenTipPosition = function(vector) {
this.penTipPosition = createVector(-vector.x,-vector.y)
}
this.draw = function() {
this.sketch.push()
this.sketch.translate(this.penTipPosition)
//draw an image as last element to override everythin an maitain the shadow effect
this.sketch.image(this.image, this.position.x, this.position.y)
this.sketch.pop()
}
this.updateToNextPosition = function(replaceSpeed) {
//show image as last element
let nextY, nextX, distanceFromX, distanceFromY, minimalDistanceFromX, minimalDistanceFromY
let definedSpeed = replaceSpeed ? replaceSpeed : this.speed
//absolute distance (ignore minus)
distanceFromX = this.sketch.abs(this.targetPosition.x - this.position.x)
distanceFromY = this.sketch.abs(this.targetPosition.y - this.position.y)
//max step available based on distance and speed
minimalDistanceFromX = this.sketch.min(definedSpeed, distanceFromX)
minimalDistanceFromY = this.sketch.min(definedSpeed, distanceFromY)
//current x is bigger
if (this.position.x > this.targetPosition.x)
nextX = this.position.x - minimalDistanceFromX
else
nextX = this.position.x + minimalDistanceFromX
//current y is bigger
if (this.position.y > this.targetPosition.y)
nextY = this.position.y - minimalDistanceFromY
else
nextY = this.position.y + minimalDistanceFromY
let nextPositionVector = this.sketch.createVector(nextX, nextY)
if (this.canSaveSteps)
this.steps.push(nextPositionVector)
//update the current position
this.setPosition(nextPositionVector)
}
}