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

Feature/fix pencil and keyboard on mobile devices #84

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ module.exports = function(grunt) {
// set default tasks to run when grunt is called without parameters
// http://gruntjs.com/api/grunt.task
grunt.registerTask('default', ['concat', 'jsbeautifier', 'uglify']);
grunt.registerTask('watch', ['watch']);
grunt.loadNpmTasks('grunt-contrib-watch');
};
11 changes: 7 additions & 4 deletions dev/events-handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var canvas = tempContext.canvas,
isTouch = 'createTouch' in document;
isTouch = 'createTouch' in document || 'ontouchstart' in window;

addEvent(canvas, isTouch ? 'touchstart mousedown' : 'mousedown', function(e) {
if (isTouch) e = e.pageX ? e : e.touches.length ? e.touches[0] : {
Expand Down Expand Up @@ -161,13 +161,15 @@ function onkeyup(e) {
keyCode = e.which || e.keyCode || 0;

if (keyCode === 13 && is.isText) {
textHandler.onReturnKeyPressed();
// no handle anumore
// textHandler.onReturnKeyPressed();
return;
}

if (keyCode == 8 || keyCode == 46) {
if (isBackKey(e, keyCode)) {
textHandler.writeText(textHandler.lastKeyPress, true);
// no handle anymore
// textHandler.writeText(textHandler.lastKeyPress, true);
}
return;
}
Expand Down Expand Up @@ -231,7 +233,8 @@ function onkeypress(e) {

var inp = String.fromCharCode(keyCode);
if (/[a-zA-Z0-9-_ !?|\/'",.=:;(){}\[\]`~@#$%^&*+-]/.test(inp)) {
textHandler.writeText(String.fromCharCode(keyCode));
// no handle anymore
// textHandler.writeText(String.fromCharCode(keyCode));
}
}

Expand Down
27 changes: 27 additions & 0 deletions dev/text-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var textHandler = {
textHandler.blinkCursorInterval = setInterval(textHandler.blinkCursor, 700);

this.showTextTools();
this.focusVirtualTextbox();
},
mouseup: function(e) {},
mousemove: function(e) {},
Expand Down Expand Up @@ -157,6 +158,32 @@ var textHandler = {
// child.style.fontSize = child.innerHTML + 'px';
});
},

/**
* In order to support mobile devices,
* or support some special language
* It will create an hidden textbox
* User will type into this textbox
*/
focusVirtualTextbox: function() {
var textbox = document.getElementById('virtual-textbox');

if (!textbox) {
textbox = document.createElement('input');
textbox.id = 'virtual-textbox';
textbox.setAttribute('type', 'text');
textbox.style.opacity = '0';

textbox.addEventListener('keyup', function(e) {
this.text = e.target.value;
this.fillText(e.target.value);
}.bind(this))
}

textbox.value = '';
document.body.append(textbox);
textbox.focus();
},
eachFontFamily: function(callback) {
var childs = this.fontFamilyBox.querySelectorAll('li');
for (var i = 0; i < childs.length; i++) {
Expand Down
69 changes: 53 additions & 16 deletions widget.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Last time updated: 2019-03-08 2:53:41 PM UTC
// Last time updated: 2020-12-21 6:53:15 AM UTC

// _______________
// Canvas-Designer
Expand Down Expand Up @@ -1939,6 +1939,7 @@
textHandler.blinkCursorInterval = setInterval(textHandler.blinkCursor, 700);

this.showTextTools();
this.focusVirtualTextbox();
},
mouseup: function(e) {},
mousemove: function(e) {},
Expand Down Expand Up @@ -1994,6 +1995,32 @@
// child.style.fontSize = child.innerHTML + 'px';
});
},

/**
* In order to support mobile devices,
* or support some special language
* It will create an hidden textbox
* User will type into this textbox
*/
focusVirtualTextbox: function() {
var textbox = document.getElementById('virtual-textbox');

if (!textbox) {
textbox = document.createElement('input');
textbox.id = 'virtual-textbox';
textbox.setAttribute('type', 'text');
textbox.style.opacity = '0';

textbox.addEventListener('keyup', function(e) {
this.text = e.target.value;
this.fillText(e.target.value);
}.bind(this))
}

textbox.value = '';
document.body.append(textbox);
textbox.focus();
},
eachFontFamily: function(callback) {
var childs = this.fontFamilyBox.querySelectorAll('li');
for (var i = 0; i < childs.length; i++) {
Expand Down Expand Up @@ -2498,12 +2525,17 @@

var zoomHandler = {
scale: 1.0,
lastZoomState: null,
up: function(e) {
this.scale = this.lastZoomState !== 'up' ? 1 : this.scale;
this.scale += .01;
this.lastZoomState = 'up';
this.apply();
},
down: function(e) {
this.scale = this.lastZoomState !== 'down' ? 1 : this.scale;
this.scale -= .01;
this.lastZoomState = 'down';
this.apply();
},
apply: function() {
Expand All @@ -2518,7 +2550,7 @@
},
down: function(ctx) {
ctx.font = '22px Verdana';
ctx.strokeText('-', 15, 30);
ctx.strokeText('-', 10, 30);
}
}
};
Expand Down Expand Up @@ -3728,7 +3760,7 @@
}

var canvas = tempContext.canvas,
isTouch = 'createTouch' in document;
isTouch = 'createTouch' in document || 'ontouchstart' in window;

addEvent(canvas, isTouch ? 'touchstart mousedown' : 'mousedown', function(e) {
if (isTouch) e = e.pageX ? e : e.touches.length ? e.touches[0] : {
Expand Down Expand Up @@ -3890,13 +3922,15 @@
keyCode = e.which || e.keyCode || 0;

if (keyCode === 13 && is.isText) {
textHandler.onReturnKeyPressed();
// no handle anumore
// textHandler.onReturnKeyPressed();
return;
}

if (keyCode == 8 || keyCode == 46) {
if (isBackKey(e, keyCode)) {
textHandler.writeText(textHandler.lastKeyPress, true);
// no handle anymore
// textHandler.writeText(textHandler.lastKeyPress, true);
}
return;
}
Expand Down Expand Up @@ -3960,7 +3994,8 @@

var inp = String.fromCharCode(keyCode);
if (/[a-zA-Z0-9-_ !?|\/'",.=:;(){}\[\]`~@#$%^&*+-]/.test(inp)) {
textHandler.writeText(String.fromCharCode(keyCode));
// no handle anymore
// textHandler.writeText(String.fromCharCode(keyCode));
}
}

Expand Down Expand Up @@ -4061,21 +4096,23 @@
}

if (index === -1) {
if (points.length && points[points.length - 1][0] === 'pencil') {
if (points.length && (points[points.length - 1][0] === 'pencil' || points[points.length - 1][0] === 'marker')) {
var newArray = [];
var length = points.length;
var reverse = points.reverse();
var ended;

/* modification start*/
var index;
for (var i = 0; i < length; i++) {
var point = reverse[i];
if (point[3] == 'start') {
ended = true;
} else if (ended) {
newArray.push(point);
}
var point = points[i];
if (point[3] === 'start') index = i;
}
var copy = [];
for (var i = 0; i < index; i++) {
copy.push(points[i]);
}
points = copy;
/*modification ends*/

points = newArray.reverse();
drawHelper.redraw();
syncPoints(true);
return;
Expand Down
8 changes: 4 additions & 4 deletions widget.min.js

Large diffs are not rendered by default.