Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pen presure to brush #180

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 43 additions & 10 deletions src/js/tools/brush.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Brush_class extends Base_tools_class {
this.name = 'brush';
this.layer = {};
this.params_hash = false;
this.pressure_supported = false;
this.pointer_pressure = 0; // has range [0 - 1]
}

dragStart(event) {
Expand Down Expand Up @@ -41,6 +43,14 @@ class Brush_class extends Base_tools_class {
load() {
var _this = this;

//pointer events
document.addEventListener('pointerdown', function (event) {
_this.pointerdown(event);
});
document.addEventListener('pointermove', function (event) {
_this.pointermove(event);
});

//mouse events
document.addEventListener('mousedown', function (event) {
_this.dragStart(event);
Expand All @@ -64,6 +74,25 @@ class Brush_class extends Base_tools_class {
});
}

pointerdown(e) {
// Devices that don't actually support pen pressure can give 0.5 as a false reading.
// It is highly unlikely a real pen will read exactly 0.5 at the start of a stroke.
if (e.pressure && e.pressure !== 0 && e.pressure !== 0.5 && e.pressure <= 1) {
this.pressure_supported = true;
this.pointer_pressure = e.pressure;
} else {
this.pressure_supported = false;
}
}

pointermove(e) {
// Pressure of exactly 1 seems to be an input error, sometimes I see it when lifting the pen
// off the screen when pressure reading should be near 0.
if (this.pressure_supported && e.pressure < 1) {
this.pointer_pressure = e.pressure;
}
}

mousedown(e) {
var mouse = this.get_mouse_info(e);
if (mouse.valid == false || mouse.click_valid == false)
Expand All @@ -89,10 +118,6 @@ class Brush_class extends Base_tools_class {
this.Base_layers.insert(this.layer);
this.params_hash = params_hash;
}
else {
//continue adding layer data, just register break
config.layer.data.push(null);
}
}

mousemove(e) {
Expand All @@ -110,10 +135,11 @@ class Brush_class extends Base_tools_class {
//detect line size
var size = params.size;
var new_size = size;
/*if(typeof e.pressure !== "undefined"){ // needs testing !!!
new_size = size * e.pressure * 2; //e.pressure has range [0 - 1]

if (this.pressure_supported) {
new_size = size * this.pointer_pressure * 2;
}
else*/ if (params.smart_brush == true) {
else if (params.smart_brush == true) {
new_size = size + size / max_speed * mouse.speed_average * power;
new_size = Math.max(new_size, size / 4);
new_size = Math.round(new_size);
Expand All @@ -132,7 +158,14 @@ class Brush_class extends Base_tools_class {
}

//more data
config.layer.data.push([mouse.x - config.layer.x, mouse.y - config.layer.y]);
var params = this.getParams();
var size = params.size;
var new_size = size;
if (this.pressure_supported) {
new_size = size * this.pointer_pressure * 2;
}
config.layer.data.push([mouse.x - config.layer.x, mouse.y - config.layer.y, new_size]);
config.layer.data.push(null);
config.layer.status = null;
this.Base_layers.render();
}
Expand Down Expand Up @@ -167,12 +200,12 @@ class Brush_class extends Base_tools_class {
//line
ctx.lineWidth = data[i][2];

if (data[i - 1] == null) {
if (data[i - 1] == null && data[i + 1] == null) {
//exception - point
ctx.arc(data[i][0], data[i][1], size / 2, 0, 2 * Math.PI, false);
ctx.fill();
}
else {
else if (data[i - 1] != null) {
//lines
ctx.lineWidth = data[i][2];
ctx.beginPath();
Expand Down