Skip to content

Commit 91fab94

Browse files
committed
feat: enhance scaling functionality to support WheelEvent and improve event handling
1 parent 223809d commit 91fab94

File tree

6 files changed

+24
-15
lines changed

6 files changed

+24
-15
lines changed

dist/index.esm.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ function calcBorderType(inTop, inRight, inBottom, inLeft, x, y, left, top, right
265265
* @param opt 回调选项
266266
*/
267267
function move(e, opt) {
268-
const target = e.target;
269268
isMoving = true;
270269
set(opt.cursor ?? getComputedStyle(e.target).cursor);
271270
let { x: tx, y: ty } = getEventPos(e);
@@ -293,7 +292,6 @@ function move(e, opt) {
293292
let objectLeft = 0, objectTop = 0, objectWidth = 0, objectHeight = 0;
294293
let offsetLeft = 0, offsetTop = 0, offsetRight = 0, offsetBottom = 0;
295294
const moveTimes = [];
296-
target.style.touchAction = 'none';
297295
down(e, {
298296
start: () => {
299297
if (opt.start?.(tx, ty) === false) {
@@ -367,7 +365,6 @@ function move(e, opt) {
367365
},
368366
end: (ne) => {
369367
opt.end?.(moveTimes, ne);
370-
target.style.touchAction = '';
371368
}
372369
});
373370
return { 'left': left, 'top': top, 'right': right, 'bottom': bottom };
@@ -718,14 +715,15 @@ function drag(e, el, opt) {
718715
* limitations under the License.
719716
*/
720717
/**
721-
* --- 绑定滚轮缩放,需要绑定到 wheel 事件上 ---
718+
* --- 绑定滚轮缩放 ---
722719
* @param oe 触发的 WheelEvent 事件
723720
* @param handler 回调函数
724721
*/
725722
function scaleWheel(oe, handler) {
726723
if (!oe.deltaY) {
727724
return;
728725
}
726+
oe.preventDefault();
729727
const delta = Math.abs(oe.deltaY);
730728
const zoomFactor = delta * (delta > 50 ? 0.0015 : 0.003);
731729
handler(oe, oe.deltaY < 0 ? 1 + zoomFactor : 1 - zoomFactor, { 'x': 0, 'y': 0 });
@@ -736,6 +734,10 @@ function scaleWheel(oe, handler) {
736734
* @param handler 回调函数
737735
*/
738736
function scale(oe, handler) {
737+
if (oe instanceof WheelEvent) {
738+
scaleWheel(oe, handler);
739+
return;
740+
}
739741
const target = oe.target;
740742
if (!target) {
741743
return;
@@ -1026,4 +1028,4 @@ function gesture(oe, before, handler) {
10261028
}
10271029
}
10281030

1029-
export { allowEvent, click, dblClick, down, drag, gesture, getData as getDragData, getEventPos, getMoveDir, isMoving, isTouch, long, move, resize, scale, scaleWheel, set as setCursor, setData as setDragData };
1031+
export { allowEvent, click, dblClick, down, drag, gesture, getData as getDragData, getEventPos, getMoveDir, isMoving, isTouch, long, move, resize, scale, set as setCursor, setData as setDragData };

dist/index.umd.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@
271271
* @param opt 回调选项
272272
*/
273273
function move(e, opt) {
274-
const target = e.target;
275274
exports.isMoving = true;
276275
set(opt.cursor ?? getComputedStyle(e.target).cursor);
277276
let { x: tx, y: ty } = getEventPos(e);
@@ -299,7 +298,6 @@
299298
let objectLeft = 0, objectTop = 0, objectWidth = 0, objectHeight = 0;
300299
let offsetLeft = 0, offsetTop = 0, offsetRight = 0, offsetBottom = 0;
301300
const moveTimes = [];
302-
target.style.touchAction = 'none';
303301
down(e, {
304302
start: () => {
305303
if (opt.start?.(tx, ty) === false) {
@@ -373,7 +371,6 @@
373371
},
374372
end: (ne) => {
375373
opt.end?.(moveTimes, ne);
376-
target.style.touchAction = '';
377374
}
378375
});
379376
return { 'left': left, 'top': top, 'right': right, 'bottom': bottom };
@@ -724,14 +721,15 @@
724721
* limitations under the License.
725722
*/
726723
/**
727-
* --- 绑定滚轮缩放,需要绑定到 wheel 事件上 ---
724+
* --- 绑定滚轮缩放 ---
728725
* @param oe 触发的 WheelEvent 事件
729726
* @param handler 回调函数
730727
*/
731728
function scaleWheel(oe, handler) {
732729
if (!oe.deltaY) {
733730
return;
734731
}
732+
oe.preventDefault();
735733
const delta = Math.abs(oe.deltaY);
736734
const zoomFactor = delta * (delta > 50 ? 0.0015 : 0.003);
737735
handler(oe, oe.deltaY < 0 ? 1 + zoomFactor : 1 - zoomFactor, { 'x': 0, 'y': 0 });
@@ -742,6 +740,10 @@
742740
* @param handler 回调函数
743741
*/
744742
function scale(oe, handler) {
743+
if (oe instanceof WheelEvent) {
744+
scaleWheel(oe, handler);
745+
return;
746+
}
745747
const target = oe.target;
746748
if (!target) {
747749
return;
@@ -1046,7 +1048,6 @@
10461048
exports.move = move;
10471049
exports.resize = resize;
10481050
exports.scale = scale;
1049-
exports.scaleWheel = scaleWheel;
10501051
exports.setCursor = set;
10511052
exports.setDragData = setData;
10521053

dist/index.umd.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ <h2>Event Testing</h2>
636636
});
637637
}
638638
scaleArea.addEventListener('pointerdown', handleScale);
639+
scaleArea.addEventListener('wheel', handleScale);
639640

640641
// --- gesture 演示 ---
641642
const gestureArea = document.getElementById('gesture-area');

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ export { move, isMoving } from './move';
4646
export { click, dblClick, long, allowEvent } from './click';
4747
export { resize } from './resize';
4848
export { drag, getData as getDragData, setData as setDragData } from './drag';
49-
export { scale, scaleWheel } from './scale';
49+
export { scale } from './scale';
5050
export { gesture } from './gesture';

src/scale.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ interface IScaleState {
3535
}
3636

3737
/**
38-
* --- 绑定滚轮缩放,需要绑定到 wheel 事件上 ---
38+
* --- 绑定滚轮缩放 ---
3939
* @param oe 触发的 WheelEvent 事件
4040
* @param handler 回调函数
4141
*/
42-
export function scaleWheel(oe: WheelEvent, handler: types.TScaleHandler): void {
42+
function scaleWheel(oe: WheelEvent, handler: types.TScaleHandler): void {
4343
if (!oe.deltaY) {
4444
return;
4545
}
46+
oe.preventDefault();
4647
const delta = Math.abs(oe.deltaY);
4748
const zoomFactor = delta * (delta > 50 ? 0.0015 : 0.003);
4849
handler(oe, oe.deltaY < 0 ? 1 + zoomFactor : 1 - zoomFactor, { 'x': 0, 'y': 0 }) as any;
@@ -53,7 +54,11 @@ export function scaleWheel(oe: WheelEvent, handler: types.TScaleHandler): void {
5354
* @param oe 触发的 PointerEvent 事件
5455
* @param handler 回调函数
5556
*/
56-
export function scale(oe: PointerEvent, handler: types.TScaleHandler): void {
57+
export function scale(oe: PointerEvent | WheelEvent, handler: types.TScaleHandler): void {
58+
if (oe instanceof WheelEvent) {
59+
scaleWheel(oe, handler);
60+
return;
61+
}
5762
const target = oe.target as HTMLElement;
5863
if (!target) {
5964
return;

0 commit comments

Comments
 (0)