diff --git a/README.md b/README.md index 0c91041..50e1800 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ ```sh https://konvajs.org/docs/groups_and_layers/Layering.html -https://konvajs.org/docs/groups_and_layers/zIndex.html https://konvajs.org/docs/tweens/Linear_Easing.html https://konvajs.org/docs/tweens/Common_Easings.html diff --git a/src/views/groups/change-container/index.vue b/src/views/groups/change-container/index.vue new file mode 100644 index 0000000..cd82e98 --- /dev/null +++ b/src/views/groups/change-container/index.vue @@ -0,0 +1,90 @@ + + + + + Move red box to blue group + Move red box to yellow group + + + + + diff --git a/src/views/groups/layering/index.vue b/src/views/groups/layering/index.vue index cd82e98..5c9f142 100644 --- a/src/views/groups/layering/index.vue +++ b/src/views/groups/layering/index.vue @@ -2,78 +2,49 @@ import { useWindowSize } from '@vueuse/core'; import { onMounted, ref } from 'vue'; import Konva from 'konva/lib'; -import { Group } from 'konva/lib/Group'; import { Rect } from 'konva/lib/shapes/Rect'; const { width, height } = useWindowSize(); -const yellowGroup = ref(); -const blueGroup = ref(); -const box = ref(); +const yellowBox = ref(); const initial = () => { const stage = new Konva.Stage({ container: 'container', width: width.value, height: height.value }); const layer = new Konva.Layer(); stage.add(layer); - yellowGroup.value = new Konva.Group({ - x: 100, - y: 100, - draggable: true, - }); + const colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple']; - blueGroup.value = new Konva.Group({ - x: 300, - y: 80, - draggable: true, - }); + for (let n = 0; n < 6; n++) { + (function () { + const i = n; + const box = new Konva.Rect({ + x: i * 30 + 210, + y: i * 18 + 40, + width: 100, + height: 50, + fill: colors[i], + stroke: 'black', + strokeWidth: 4, + draggable: true, + name: colors[i], + }); - box.value = new Konva.Rect({ - x: 10, - y: 10, - width: 100, - height: 50, - fill: 'red', - stroke: 'black', - }); + box.on('mouseover', function () { + document.body.style.cursor = 'pointer'; + }); + box.on('mouseout', function () { + document.body.style.cursor = 'default'; + }); + if (colors[i] === 'yellow') { + yellowBox.value = box; + } + layer.add(box); + })(); + } - const yellowCircle = new Konva.Circle({ - x: 0, - y: 0, - radius: 50, - fill: 'yellow', - stroke: 'black', - }); - - const blueCircle = new Konva.Circle({ - x: 0, - y: 0, - radius: 50, - fill: 'blue', - stroke: 'black', - }); - - yellowGroup.value.add(yellowCircle); - yellowGroup.value.add(box.value); - blueGroup.value.add(blueCircle); - layer.add(yellowGroup.value); - layer.add(blueGroup.value); stage.add(layer); }; -/** - * * 移动到蓝色组 - */ -const toBlue = () => { - box.value?.moveTo(blueGroup.value); -}; - -/** - * * 移动到黄色 - */ -const toYellow = () => { - box.value?.moveTo(yellowGroup.value); -}; - onMounted(() => { initial(); }); @@ -81,8 +52,13 @@ onMounted(() => { - Move red box to blue group - Move red box to yellow group + + Move yellow box to top + Move yellow box to bottom + Move yellow box up + Move yellow box down + Set yellow box zIndex to 3 +