Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from aesoper101/feat/2.1.5
Feat/2.1.5
- Loading branch information
Showing
9 changed files
with
243 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { computed, ref, watch, onMounted, defineComponent } from "vue"; | ||
import { calcAngle, triggerDragEvent, DragEventOption } from "./utils"; | ||
|
||
import "./index.scss"; | ||
|
||
const angleProps = { | ||
angle: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
size: { | ||
type: Number, | ||
default: 16, | ||
validator: (value: number) => { | ||
return value >= 16; | ||
}, | ||
}, | ||
borderWidth: { | ||
type: Number, | ||
default: 1, | ||
validator: (value: number) => { | ||
return value >= 1; | ||
}, | ||
}, | ||
borderColor: { | ||
type: String, | ||
default: "#666", | ||
}, | ||
}; | ||
|
||
export default defineComponent({ | ||
name: "Angle", | ||
props: angleProps, | ||
emits: ["update:angle", "change"], | ||
setup(props, { emit }) { | ||
const angleRef = ref<HTMLElement | null>(null); | ||
|
||
const rotate = ref(0); | ||
|
||
watch( | ||
() => props.angle, | ||
(angle: number) => { | ||
rotate.value = angle; | ||
} | ||
); | ||
|
||
const updateAngle = (): void => { | ||
let value = Number(rotate.value); | ||
if (!isNaN(value)) { | ||
value = value > 360 || value < 0 ? props.angle : value; | ||
rotate.value = value === 360 ? 0 : value; | ||
emit("update:angle", rotate.value); | ||
emit("change", rotate.value); | ||
} | ||
}; | ||
|
||
const getStyle = computed(() => { | ||
return { | ||
width: props.size + "px", | ||
height: props.size + "px", | ||
borderWidth: props.borderWidth + "px", | ||
borderColor: props.borderColor, | ||
transform: `rotate(${rotate.value}deg)`, | ||
}; | ||
}); | ||
|
||
const handleDrag = (event: MouseEvent) => { | ||
if (angleRef.value) { | ||
rotate.value = calcAngle(angleRef.value, event) % 360; | ||
updateAngle(); | ||
} | ||
}; | ||
|
||
onMounted(() => { | ||
const dragConfig: DragEventOption = { | ||
drag: (event: Event) => { | ||
handleDrag(event as MouseEvent); | ||
}, | ||
end: (event: Event) => { | ||
handleDrag(event as MouseEvent); | ||
}, | ||
}; | ||
|
||
if (angleRef.value) { | ||
triggerDragEvent(angleRef.value, dragConfig); | ||
} | ||
}); | ||
|
||
return () => { | ||
return ( | ||
<div class="bee-angle"> | ||
<div class="bee-angle__round" ref={angleRef} style={getStyle.value} /> | ||
</div> | ||
); | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
.bee-angle { | ||
transform: rotate(90deg); | ||
|
||
> * { | ||
display: inline-block; | ||
vertical-align: middle; | ||
} | ||
|
||
&.zero-right { | ||
transform: rotate(180deg); | ||
} | ||
|
||
&.zero-bottom { | ||
transform: rotate(270deg); | ||
} | ||
|
||
&__round { | ||
position: relative; | ||
margin-left: 8px; | ||
width: 16px; | ||
height: 16px; | ||
border: 1px solid #666; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
display: inline-block; | ||
vertical-align: middle; | ||
|
||
&::before { | ||
content: ""; | ||
position: absolute; | ||
top: 50%; | ||
left: 0; | ||
transform: translateY(-50%); | ||
width: 50%; | ||
height: 1px; | ||
background-color: #666; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
export const calcAngle = (element: HTMLElement, event: MouseEvent): number => { | ||
const rect = element.getBoundingClientRect(); | ||
|
||
const originX = rect.left + rect.width / 2; | ||
const originY = rect.top + rect.height / 2; | ||
|
||
//获得中心点和鼠标坐标连线,与y轴正半轴之间的夹角 | ||
const x = Math.abs(originX - event.clientX); | ||
const y = Math.abs(originY - event.clientY); | ||
const z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); | ||
const cos = y / z; | ||
const rad = Math.acos(cos); //用反三角函数求弧度 | ||
let angle = Math.floor(180 / (Math.PI / rad)); //将弧度转换成角度 | ||
|
||
if (event.clientX > originX && event.clientY > originY) { | ||
//鼠标在第四象限 | ||
angle = 180 - angle; | ||
} | ||
|
||
if (event.clientX == originX && event.clientY > originY) { | ||
//鼠标在y轴负方向上 | ||
angle = 180; | ||
} | ||
|
||
if (event.clientX > originX && event.clientY == originY) { | ||
//鼠标在x轴正方向上 | ||
angle = 90; | ||
} | ||
|
||
if (event.clientX < originX && event.clientY > originY) { | ||
//鼠标在第三象限 | ||
angle = 180 + angle; | ||
} | ||
|
||
if (event.clientX < originX && event.clientY == originY) { | ||
//鼠标在x轴负方向 | ||
angle = 270; | ||
} | ||
|
||
if (event.clientX < originX && event.clientY < originY) { | ||
//鼠标在第二象限 | ||
angle = 360 - angle; | ||
} | ||
|
||
return angle; | ||
}; | ||
|
||
let isDragging = false; | ||
|
||
export interface DragEventOption { | ||
drag?: (event: Event) => void; | ||
start?: (event: Event) => void; | ||
end?: (event: Event) => void; | ||
} | ||
|
||
export const triggerDragEvent = (element: HTMLElement, options: DragEventOption): void => { | ||
const moveFn = function (event: Event) { | ||
options.drag?.(event); | ||
}; | ||
|
||
const upFn = function (event: Event) { | ||
document.removeEventListener("mousemove", moveFn, false); | ||
document.removeEventListener("mouseup", upFn, false); | ||
document.onselectstart = null; | ||
document.ondragstart = null; | ||
|
||
isDragging = false; | ||
|
||
options.end?.(event); | ||
}; | ||
|
||
if (element) { | ||
element.addEventListener("mousedown", (event: Event) => { | ||
if (isDragging) return; | ||
document.onselectstart = () => false; | ||
document.ondragstart = () => false; | ||
document.addEventListener("mousemove", moveFn, false); | ||
document.addEventListener("mouseup", upFn, false); | ||
isDragging = true; | ||
|
||
options.start?.(event); | ||
}); | ||
} | ||
|
||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters