HTML5 Canvas 线段连接教程
要设置 Konva 绘图形状的线段连接样式,我们可以在实例化形状时设置 lineJoin
属性,或者使用 lineJoin()
方法。
lineJoin
属性可以设置为 miter
、bevel
或 round
。除非另有说明,默认的线段连接样式为 miter
。
说明: 鼠标悬停在三角形上更改线段连接样式。
- Vanilla
- React
- Vue
import Konva from 'konva'; const stage = new Konva.Stage({ container: 'container', width: window.innerWidth, height: window.innerHeight, }); const layer = new Konva.Layer(); stage.add(layer); const triangle = new Konva.RegularPolygon({ x: stage.width() / 2, y: stage.height() / 2, sides: 3, radius: 70, fill: '#00D2FF', stroke: 'black', strokeWidth: 20, lineJoin: 'miter' }); layer.add(triangle); triangle.on('mouseenter', function() { const lineJoins = ['miter', 'bevel', 'round']; const index = lineJoins.indexOf(triangle.lineJoin()); const nextIndex = (index + 1) % lineJoins.length; triangle.lineJoin(lineJoins[nextIndex]); });
import { Stage, Layer, RegularPolygon } from 'react-konva'; import { useState } from 'react'; const App = () => { const [lineJoin, setLineJoin] = useState('miter'); const handleMouseEnter = () => { const lineJoins = ['miter', 'bevel', 'round']; const index = lineJoins.indexOf(lineJoin); const nextIndex = (index + 1) % lineJoins.length; setLineJoin(lineJoins[nextIndex]); }; return ( <Stage width={window.innerWidth} height={window.innerHeight}> <Layer> <RegularPolygon x={window.innerWidth / 2} y={window.innerHeight / 2} sides={3} radius={70} fill="#00D2FF" stroke="black" strokeWidth={20} lineJoin={lineJoin} onMouseEnter={handleMouseEnter} /> </Layer> </Stage> ); }; export default App;
<template> <v-stage :config="stageSize"> <v-layer> <v-regular-polygon :config="triangleConfig" @mouseenter="handleMouseEnter" /> </v-layer> </v-stage> </template> <script setup> import { ref } from 'vue'; const stageSize = { width: window.innerWidth, height: window.innerHeight }; const currentLineJoin = ref('miter'); const triangleConfig = { x: window.innerWidth / 2, y: window.innerHeight / 2, sides: 3, radius: 70, fill: '#00D2FF', stroke: 'black', strokeWidth: 20, lineJoin: currentLineJoin.value }; const handleMouseEnter = () => { const lineJoins = ['miter', 'bevel', 'round']; const index = lineJoins.indexOf(currentLineJoin.value); const nextIndex = (index + 1) % lineJoins.length; currentLineJoin.value = lineJoins[nextIndex]; }; </script>