Skip to content

Commit

Permalink
Release (#1680)
Browse files Browse the repository at this point in the history
* Correct mouse position even when scaling the container using CSS (#1678)

* fix: calculate scale when css transform enabled #1677

* chore: commit changeset

* chore(release): bump version (#1679)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 6, 2024
1 parent 3597b79 commit e97c686
Show file tree
Hide file tree
Showing 99 changed files with 536 additions and 82 deletions.
57 changes: 57 additions & 0 deletions __tests__/demos/bugfix/1677.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Canvas, Circle } from '../../../packages/g';
import { Renderer } from '../../../packages/g-canvas';

export async function shadowroot_offset(context) {
const { container } = context;
container.innerHTML = '';

class CustomElement extends HTMLElement {
connectedCallback() {
const shadowRoot = this.attachShadow({ mode: 'open' });
// 向 shodowRoot 中添加一些内容
shadowRoot.innerHTML = `
<div style='display: flex; transform: scale(1.5) translate(100px, 0);'>
<div style="display: flex; width: 200px; height: 400px; border-right: 1px solid rgb(221, 221, 221);"></div>
<div style="flex: 1 1 0%; display: flex;">
<div id='container' style='height: 300px'></div>
</div>
</div>
`;

const canvas = new Canvas({
container: shadowRoot.querySelector('#container')! as HTMLElement,
width: 500,
height: 500,
renderer: new Renderer(),
supportsCSSTransform: true,
});

const circle = new Circle({
style: {
cx: 100,
cy: 100,
r: 50,
fill: 'red',
cursor: 'pointer',
},
});
canvas.appendChild(circle);

circle.addEventListener('pointerenter', () => {
circle.style.fill = 'green';
});
circle.addEventListener('pointerleave', () => {
circle.style.fill = 'red';
});
}

disconnectedCallback() {}
}

if (!customElements.get('error-element')) {
customElements.define('error-element', CustomElement);
}

const e = document.createElement('error-element');
container?.appendChild(e);
}
1 change: 1 addition & 0 deletions __tests__/demos/bugfix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { transform_path } from './1624';
export { scale0 } from './scale0';
export { dirty } from './dirty';
export { image } from './1636';
export { shadowroot_offset } from './1677';
7 changes: 7 additions & 0 deletions packages/g-camera-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-camera-api

## 2.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-camera-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-camera-api",
"version": "2.0.2",
"version": "2.0.3",
"description": "A simple implementation of Camera API.",
"keywords": [
"antv",
Expand Down
13 changes: 13 additions & 0 deletions packages/g-canvas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @antv/g-canvas

## 2.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3
- @antv/g-plugin-canvas-path-generator@2.0.3
- @antv/g-plugin-canvas-picker@2.0.3
- @antv/g-plugin-canvas-renderer@2.0.3
- @antv/g-plugin-dom-interaction@2.0.3
- @antv/g-plugin-html-renderer@2.0.3
- @antv/g-plugin-image-loader@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-canvas/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-canvas",
"version": "2.0.2",
"version": "2.0.3",
"description": "A renderer implemented by Canvas 2D API",
"keywords": [
"antv",
Expand Down
13 changes: 13 additions & 0 deletions packages/g-canvaskit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @antv/g-canvaskit

## 1.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3
- @antv/g-plugin-canvas-path-generator@2.0.3
- @antv/g-plugin-canvas-picker@2.0.3
- @antv/g-plugin-canvaskit-renderer@2.0.3
- @antv/g-plugin-dom-interaction@2.0.3
- @antv/g-plugin-html-renderer@2.0.3
- @antv/g-plugin-image-loader@2.0.3

## 1.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-canvaskit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-canvaskit",
"version": "1.0.2",
"version": "1.0.3",
"description": "A renderer implemented by CanvasKit",
"keywords": [
"antv",
Expand Down
7 changes: 7 additions & 0 deletions packages/g-components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-components

## 2.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-components",
"version": "2.0.2",
"version": "2.0.3",
"description": "Components for g",
"keywords": [
"antv",
Expand Down
7 changes: 7 additions & 0 deletions packages/g-dom-mutation-observer-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-dom-mutation-observer-api

## 2.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-dom-mutation-observer-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-dom-mutation-observer-api",
"version": "2.0.2",
"version": "2.0.3",
"description": "A simple implementation of DOM MutationObserver API.",
"keywords": [
"antv",
Expand Down
7 changes: 7 additions & 0 deletions packages/g-gesture/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-gesture

## 3.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 3.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-gesture/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-gesture",
"version": "3.0.2",
"version": "3.0.3",
"description": "G Gesture",
"keywords": [
"antv",
Expand Down
7 changes: 7 additions & 0 deletions packages/g-image-exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-image-exporter

## 1.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 1.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-image-exporter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-image-exporter",
"version": "1.0.2",
"version": "1.0.3",
"description": "A image exporter for G using DOM API",
"keywords": [
"antv",
Expand Down
6 changes: 6 additions & 0 deletions packages/g-lite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @antv/g-lite

## 2.0.3

### Patch Changes

- 36521463: Correct mouse position even when scaling the container using CSS.

## 2.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-lite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-lite",
"version": "2.0.2",
"version": "2.0.3",
"description": "A core module for rendering engine implements DOM API.",
"keywords": [
"antv",
Expand Down
39 changes: 9 additions & 30 deletions packages/g-lite/src/plugins/EventPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { isNil, isUndefined } from '@antv/util';
import { isUndefined } from '@antv/util';
import type { FederatedMouseEvent, ICanvas, IEventTarget } from '../dom';
import { FederatedPointerEvent } from '../dom/FederatedPointerEvent';
import { FederatedWheelEvent } from '../dom/FederatedWheelEvent';
import type { RenderingPlugin, RenderingPluginContext } from '../services';
import { Point } from '../shapes';
import type { Cursor, EventPosition, InteractivePointerEvent } from '../types';
import type { FormattedPointerEvent, FormattedTouch } from '../utils/event';
import { MOUSE_POINTER_ID, TOUCH_TO_POINTER, clock } from '../utils/event';
Expand Down Expand Up @@ -195,32 +194,6 @@ export class EventPlugin implements RenderingPlugin {
this.setCursor(this.context.eventService.cursor);
};

private getViewportXY(nativeEvent: PointerEvent | WheelEvent) {
let x: number;
let y: number;
/**
* Should account for CSS Transform applied on container.
* @see https://github.com/antvis/G/issues/1161
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/offsetX
*/
const { offsetX, offsetY, clientX, clientY } = nativeEvent;
if (
this.context.config.supportsCSSTransform &&
!isNil(offsetX) &&
!isNil(offsetY)
) {
x = offsetX;
y = offsetY;
} else {
const point = this.context.eventService.client2Viewport(
new Point(clientX, clientY),
);
x = point.x;
y = point.y;
}
return { x, y };
}

private bootstrapEvent(
event: FederatedPointerEvent,
normalizedEvent: PointerEvent,
Expand All @@ -243,7 +216,10 @@ export class EventPlugin implements RenderingPlugin {
event.twist = normalizedEvent.twist;
this.transferMouseData(event, normalizedEvent);

const { x, y } = this.getViewportXY(normalizedEvent);
const { x, y } = this.context.eventService.client2Viewport({
x: normalizedEvent.clientX,
y: normalizedEvent.clientY,
});
event.viewport.x = x;
event.viewport.y = y;
const { x: canvasX, y: canvasY } =
Expand Down Expand Up @@ -276,7 +252,10 @@ export class EventPlugin implements RenderingPlugin {
event.deltaY = nativeEvent.deltaY;
event.deltaZ = nativeEvent.deltaZ;

const { x, y } = this.getViewportXY(nativeEvent);
const { x, y } = this.context.eventService.client2Viewport({
x: nativeEvent.clientX,
y: nativeEvent.clientY,
});
event.viewport.x = x;
event.viewport.y = y;
const { x: canvasX, y: canvasY } =
Expand Down
38 changes: 34 additions & 4 deletions packages/g-lite/src/services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,44 @@ export class EventService {
this.eventPool.clear();
}

client2Viewport(client: PointLike): PointLike {
private getScale() {
const bbox = this.context.contextService.getBoundingClientRect();
return new Point(client.x - (bbox?.left || 0), client.y - (bbox?.top || 0));
let scaleX = 1;
let scaleY = 1;
const $el =
this.context.contextService.getDomElement() as HTMLCanvasElement;
if ($el && bbox) {
const { offsetWidth, offsetHeight } = $el;
scaleX = bbox.width / offsetWidth;
scaleY = bbox.height / offsetHeight;
}
return {
scaleX,
scaleY,
bbox,
};
}

/**
* Should account for CSS Transform applied on container.
* @see https://github.com/antvis/G/issues/1161
* @see https://github.com/antvis/G/issues/1677
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/offsetX
*/
client2Viewport(client: PointLike): PointLike {
const { scaleX, scaleY, bbox } = this.getScale();
return new Point(
(client.x - (bbox?.left || 0)) / scaleX,
(client.y - (bbox?.top || 0)) / scaleY,
);
}

viewport2Client(canvas: PointLike): PointLike {
const bbox = this.context.contextService.getBoundingClientRect();
return new Point(canvas.x + (bbox?.left || 0), canvas.y + (bbox?.top || 0));
const { scaleX, scaleY, bbox } = this.getScale();
return new Point(
(canvas.x + (bbox?.left || 0)) * scaleX,
(canvas.y + (bbox?.top || 0)) * scaleY,
);
}

viewport2Canvas({ x, y }: PointLike): PointLike {
Expand Down
3 changes: 2 additions & 1 deletion packages/g-lite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,8 @@ export interface CanvasConfig {
disableRenderHooks?: boolean;

/**
* Should we account for CSS Transform applied on container?
* Should we account for CSS Transform applied on container? Enabled by default.
* @deprecated
*/
supportsCSSTransform?: boolean;

Expand Down
7 changes: 7 additions & 0 deletions packages/g-lottie-player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @antv/g-lottie-player

## 1.0.3

### Patch Changes

- Updated dependencies [36521463]
- @antv/g-lite@2.0.3

## 1.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/g-lottie-player/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g-lottie-player",
"version": "1.0.2",
"version": "1.0.3",
"description": "A lottie player for G",
"keywords": [
"antv",
Expand Down
Loading

0 comments on commit e97c686

Please sign in to comment.