-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
130 lines (112 loc) · 3.17 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import './style.css'
import { fabric } from 'fabric'
import VerticalTextbox from './VerticalTextbox';
import Canvas from './Canvas';
const canvas = new Canvas('c');
const btnFlip = document.getElementById('ButtonFlip');
const text = '熊玩\nヌ日池」極健リ\nabc\nhello))健 1234 名8食ー教策12ぜ'
// const text = '(abc)こbra\ncket]日「ム」\n極ー右';
let style = {
"fill": "#292929",
"editable": true,
"fontSize": 40,
"left": 100,
"top": 50,
"width": 400,
"height": 400,
};
// let funcName = 'calcTextWidth';
// let cls = 'IText';
// fabric[cls].prototype[funcName] = ((originfc) => {
// return function () {
// const result = originfc.apply(this, arguments);
// console.log({ result }, this.width)
// return result;
// }
// })(fabric[cls].prototype[funcName])
const cjkText = new VerticalTextbox(text, style);
const textbox = new fabric.Textbox(text, Object.assign(style, {
left: 500
}));
function handleTextFlipped(txtbox, originTxtBox) {
const originIndex = canvas.getObjects().indexOf(originTxtBox);
canvas.startEditing();
canvas.insertAt(txtbox, originIndex, true);
canvas.stopEditing();
canvas.setActiveObject(txtbox);
}
btnFlip.onclick = () => {
const activeObject = canvas.getActiveObject();
console.log('[x] active-objects', activeObject);
if (activeObject.type === 'vertical-textbox') {
activeObject.toTextbox(txtbox => handleTextFlipped(txtbox, activeObject))
} else if (activeObject.type === 'textbox') {
VerticalTextbox.fromTextbox(activeObject, txtbox => handleTextFlipped(txtbox, activeObject))
}
}
canvas.add(cjkText)
canvas.add(textbox)
function updateStyles() {
if (cjkText.isEditing) {
cjkText.setSelectionStyles(style)
}
if (textbox.isEditing) {
textbox.setSelectionStyles(style)
}
}
window.addEventListener('keydown', (kbEvt) => {
if (kbEvt.ctrlKey) {
let isHandled = false;
if (kbEvt.code === 'KeyZ') {
if (kbEvt.shiftKey) {
canvas.redo();
} else {
canvas.undo();
}
}
// style.fontFamily = 'gothic'
// style.fontSize = 50;
// style.linethrough = true;
// style.overline = true;
if (kbEvt.code === 'KeyB') {
style.fontWeight = style.fontWeight === 'bold' ? 'normal' : 'bold';
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'Digit0') {
style.textBackgroundColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'KeyU') {
style.underline = !style.underline;
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'KeyG') {
style.linethrough = !style.linethrough;
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'KeyE') {
style.overline = !style.overline;
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'Equal') {
style.fontSize += 2;
updateStyles();
isHandled = true;
}
if (kbEvt.code === 'Minus') {
style.fontSize -= 2;
updateStyles();
isHandled = true;
}
if (isHandled) {
kbEvt.preventDefault();
kbEvt.stopPropagation();
}
canvas.requestRenderAll();
}
})