From 9dc19dde1dcbcd92d2da86a789002223e733c2fe Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 1 Jan 2020 17:51:14 -0800 Subject: [PATCH] Proper text sizing --- src/index.js | 89 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/src/index.js b/src/index.js index 2db0ca45..c7526ce9 100644 --- a/src/index.js +++ b/src/index.js @@ -15,7 +15,6 @@ function newElement(type, x, y) { height: 0, isSelected: false }; - generateShape(element); return element; } @@ -34,8 +33,10 @@ var generator = rough.generator(); function generateShape(element) { if (element.type === "selection") { element.draw = (rc, context) => { + const fillStyle = context.fillStyle; context.fillStyle = "rgba(0, 0, 255, 0.10)"; context.fillRect(element.x, element.y, element.width, element.height); + context.fillStyle = fillStyle; }; } else if (element.type === "rectangle") { const shape = generator.rectangle( @@ -85,29 +86,37 @@ function generateShape(element) { }; return; } else if (element.type === "text") { - if (element.text === undefined) { - element.text = prompt("What text do you want?"); - } element.draw = (rc, context) => { - context.font = "20px Virgil"; - const measure = context.measureText(element.text); + const font = context.font; + context.font = element.font; const height = - measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent; + element.measure.actualBoundingBoxAscent + + element.measure.actualBoundingBoxDescent; context.fillText( element.text, - element.x - measure.width / 2, - element.y + measure.actualBoundingBoxAscent - height / 2 + element.x, + element.y + 2 * element.measure.actualBoundingBoxAscent - height / 2 ); + context.font = font; }; } else { throw new Error("Unimplemented type " + element.type); } } +function setSelection(selection) { + elements.forEach(element => { + element.isSelected = + selection.x < element.x && + selection.y < element.y && + selection.x + selection.width > element.x + element.width && + selection.y + selection.height > element.y + element.height; + }); +} + function App() { const [draggingElement, setDraggingElement] = React.useState(null); const [elementType, setElementType] = React.useState("selection"); - const [selectedElements, setSelectedElements] = React.useState([]); function ElementOption({ type, children }) { return (