Skip to content

Instantly share code, notes, and snippets.

@sschendel
Last active October 22, 2018 20:47
Show Gist options
  • Save sschendel/16f8772314320c3b650ea8c2eda937a7 to your computer and use it in GitHub Desktop.
Save sschendel/16f8772314320c3b650ea8c2eda937a7 to your computer and use it in GitHub Desktop.
fabricjs IText selectable property changes to true when exit editing occurs
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport"
content="initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-touch-fullscreen" content="yes"/>
<meta name="msapplication-tap-highlight" content="no"/>
<title>Android: LLP Whiteboard</title>
<link rel="stylesheet" type="text/css" href="css/llp_whiteboard_style.css">
<script src="scripts/libs/fabric.js"></script>
</head>
<body>
<canvas id="canvas" width="1600" height="1600"></canvas>
<script>
var currentEditing = null;
var canvas = new fabric.Canvas("canvas");
canvas.on("mouse:down", handleMouseDown);
canvas.on("text:editing:entered", handleTextEditingOn);
canvas.on("text:editing:exited", handleTextEditingOff);
//canvas.isDrawingMode = true;
var text = new fabric.IText("hello world", { left: 100, top: 100, selectable: false });
canvas.add(text);
currentEditing = text;
text.enterEditing();
function handleMouseDown(options) {
console.log(JSON.stringify(options));
if (options.target == null) {
var text = buildTextObject(
'new text',
options.absolutePointer.x,
options.absolutePointer.y
);
canvas.add(text);
text.enterEditing();
currentEditing = text;
} else {
console.log('mouse down has target! type:' + options.target.type + ' selectable? ' + options.target.get('selectable'));
}
}
function buildTextObject(textValue, x, y) {
var options = {
left: x,
top: y,
selectable: false
};
if (this.textColor != null) {
options['fill'] = this.textColor;
}
if (this.fontSize != null) {
options['fontSize'] = this.fontSize;
}
console.log("text options:" + JSON.stringify(options));
var text = new fabric.IText(textValue, options);
return text;
}
function handleTextEditingOn(options) {
console.log('handleTextEditingOn type:' + options.target.type + ' selectable? ' + options.target.get('selectable'));
}
function handleTextEditingOff(options) {
console.log('handleTextEditingOff type:' + options.target.type + ' selectable? ' + options.target.get('selectable'));
options.target.set('selectable', false);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment