xxxxxxxxxx
91
// Mask Function
p5.Graphics.prototype.mask = function(inputMask) {
if (inputMask === undefined) {
inputMask = this;
}
const currBlend = this._renderer.drawingContext.globalCompositeOperation;
let scaleFactor = 1;
if (inputMask instanceof p5.Renderer) {
scaleFactor = inputMask._pInst._pixelDensity;
}
const copyArgs = [
inputMask,
0,
0,
scaleFactor * inputMask.width,
scaleFactor * inputMask.height,
0,
0,
this.width,
this.height
];
this._renderer.drawingContext.globalCompositeOperation = 'destination-in';
// p5.Renderer2D.prototype.copy.apply(this, copyArgs);
inputMask.loadPixels();
const s = inputMask.canvas.width / inputMask.width;
console.log(this);
// this._renderer.drawingContext.drawImage(
// copyArgs[0].canvas,
// s * copyArgs[1],
// s * copyArgs[2],
// s * copyArgs[3],
// s * copyArgs[4],
// copyArgs[5],
// copyArgs[6],
// copyArgs[7],
// copyArgs[9]
// );
this._renderer.drawingContext.globalCompositeOperation = currBlend;
}
// -------------------------
// Sketch
let maskGraphics;
let drawingGraphics;
function setup() {
console.log(this._renderer);
createCanvas(400, 400, WEBGL);
maskGraphics = createGraphics(width, height, WEBGL);
drawingGraphics = createGraphics(width, height, WEBGL);
}
function draw() {
background(0);
maskGraphics.clear();
maskGraphics.normalMaterial();
maskGraphics.translate(0, 0, 0);
maskGraphics.push();
maskGraphics.rotateZ(frameCount * 0.01);
maskGraphics.rotateX(frameCount * 0.01);
maskGraphics.rotateY(frameCount * 0.01);
maskGraphics.torus(70, 20);
maskGraphics.pop();
//image(maskGraphics, 0, 0);
//drawingGraphics.mask(maskGraphics);
drawingGraphics.background(0);
drawingGraphics.normalMaterial();
drawingGraphics.translate(0, 0, 0);
drawingGraphics.push();
drawingGraphics.rotateZ(frameCount * 0.01);
drawingGraphics.rotateX(frameCount * 0.01);
drawingGraphics.rotateY(frameCount * 0.01);
drawingGraphics.cone(70, 70);
drawingGraphics.pop();
image(drawingGraphics, 0, 0);
}