Skip to content

Commit

Permalink
Add fillstyle canvas support
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed May 20, 2020
1 parent 97ebe8c commit 44d4e6d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion blueprint/core/blueprint_CanvasView.h
Expand Up @@ -64,6 +64,17 @@ namespace blueprint
auto* jsContext = new juce::DynamicObject();
juce::Path jsPath;

jsContext->setProperty("__setFillStyle", juce::var::NativeFunction {
[&g, &jsPath](const juce::var::NativeFunctionArgs& args) -> juce::var {
jassert(args.numArguments == 1);

auto hex = args.arguments[0].toString();
g.setColour(juce::Colour::fromString(hex));

return juce::var();
}
});

jsContext->setProperty("fillRect", juce::var::NativeFunction {
[&g, &jsPath](const juce::var::NativeFunctionArgs& args) -> juce::var {
jassert(args.numArguments == 4);
Expand All @@ -73,7 +84,6 @@ namespace blueprint
const int width = args.arguments[2];
const int height = args.arguments[3];

g.setColour(juce::Colours::blue);
g.fillRect(x, y, width, height);

return juce::var();
Expand Down
5 changes: 5 additions & 0 deletions examples/GainPlugin/Source/jsui/src/App.js
Expand Up @@ -13,7 +13,12 @@ import {
function animatedDraw(ctx) {
let now = (Date.now() / 10);
let width = now % 100;
let red = Math.sqrt(width / 100) * 255;
let hex = Math.floor(red).toString(16);

// TODO: Should update the ctx proxy to convert from javascript hex strings, aka

This comment has been minimized.

Copy link
@JoshMarler

JoshMarler May 20, 2020

Owner

Heh. Yeah this would be awesome

This comment has been minimized.

Copy link
@nick-thompson

nick-thompson May 20, 2020

Author Collaborator

Hahah I know :) I think it should be easy enough to roll someting like this in: https://github.com/Qix-/color and do a pretty generic "support all color types but conver to juce-flavor in the backend"

// #ffffaa to juce's Colour::fromString() API which is ffffffaa.
ctx.fillStyle = `ff${hex}ffaa`;
ctx.fillRect(0, 0, width, 2);
}

Expand Down
22 changes: 22 additions & 0 deletions packages/juce-blueprint/esm/Blueprint.js
Expand Up @@ -23,6 +23,28 @@ export function Image(props) {
}

export function Canvas(props) {
if (props.hasOwnProperty('onDraw')) {
let userOnDraw = props.onDraw;
let wrappedDraw = function(ctx) {
Object.defineProperty(ctx, 'fillStyle', {
enumerable: false,
configurable: false,
get: function() {
return 'Not Supported';
},
set: function(value) {
this.__setFillStyle(value);
}
});

return userOnDraw(ctx);
};

return React.createElement('CanvasView', Object.assign({}, props, {
onDraw: wrappedDraw,
}), props.children);
}

return React.createElement('CanvasView', props, props.children);
}

Expand Down

0 comments on commit 44d4e6d

Please sign in to comment.