<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/[email protected]/konva.min.js"></script> <meta charset="utf-8" /> <title>Konva Resize Text Demo</title> <style> body { margin: 0; padding: 0; overflow: hidden; background-color: #f0f0f0; } </style> </head>
<body> <div id="container"></div> <script> var width = window.innerWidth; var height = window.innerHeight;
var stage = new Konva.Stage({ container: 'container', width: width, height: height, });
var layer = new Konva.Layer(); stage.add(layer);
var text = new Konva.Text({ x: 50, y: 60, fontSize: 20, text: 'Hello from the Konva framework. Try to resize me.', draggable: true, }); layer.add(text);
var MIN_WIDTH = 20; var tr = new Konva.Transformer({ nodes: [text], padding: 5, enabledAnchors: ['middle-left', 'middle-right'], boundBoxFunc: (oldBox, newBox) => { if (newBox.width < MIN_WIDTH) { return oldBox; } return newBox; }, }); layer.add(tr); layer.draw();
text.on('transform', () => { text.setAttrs({ width: Math.max(text.width() * text.scaleX(), MIN_WIDTH), scaleX: 1, scaleY: 1, }); }); </script> </body> </html>
|