Skip to content

Commit

Permalink
refactor: ssr support devicePixelRatio (#6647)
Browse files Browse the repository at this point in the history
* refactor: ssr support config devicePixelRatio

* chore: update version

* refactor: update test snapshots
  • Loading branch information
Aarebecca authored Dec 18, 2024
1 parent b23ff3d commit 94b2ac3
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 135 deletions.
Binary file modified packages/g6-ssr/__tests__/assets/file.pdf
Binary file not shown.
260 changes: 130 additions & 130 deletions packages/g6-ssr/__tests__/assets/file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/g6-ssr/__tests__/assets/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/g6-ssr/__tests__/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added packages/g6-ssr/__tests__/assets/image_x1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 17 additions & 3 deletions packages/g6-ssr/__tests__/graph.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import { join } from 'path';
import type { Graph, MetaData } from '../src';
import { createGraph } from '../src';
Expand All @@ -19,8 +19,11 @@ expect.extend({
const file = received.toBuffer(meta);
const pass = existsSync(_path) ? file.equals(readFileSync(_path)) : true;

const actualName = _path.replace('.', '-actual.');
if (!pass) {
writeFileSync(_path.replace('.', '-actual.'), file);
writeFileSync(actualName, file);
} else if (existsSync(actualName)) {
unlinkSync(actualName);
}

if (pass) {
Expand All @@ -38,7 +41,7 @@ expect.extend({
});

describe('createGraph', () => {
const fn = async (outputType?: any, imageType: any = 'png') => {
const fn = async (outputType?: any, imageType: any = 'png', options = {}) => {
const data = {
nodes: [
{ id: '0' },
Expand Down Expand Up @@ -158,6 +161,7 @@ describe('createGraph', () => {
layout: {
type: 'circular',
},
...options,
});
};

Expand Down Expand Up @@ -210,4 +214,14 @@ describe('createGraph', () => {

graph.destroy();
});

it('devicePixelRatio', async () => {
const graph = await fn('image', 'jpeg', { devicePixelRatio: 1 });

expect(graph).toMatchFile('./assets/image_x1.jpeg');

graph.exportToFile(join(__dirname, './assets/image_x1'));

graph.destroy();
});
});
2 changes: 1 addition & 1 deletion packages/g6-ssr/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/g6-ssr",
"version": "0.0.2",
"version": "0.0.3",
"description": "Support SSR for G6",
"keywords": [
"antv",
Expand Down
3 changes: 2 additions & 1 deletion packages/g6-ssr/src/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Options } from './types';
* @returns <zh/> [G6 画布, NodeCanvas 画布] | <en/> [G6Canvas, NodeCanvas]
*/
export function createCanvas(options: Options): [G6Canvas, NodeCanvas] {
const { width, height, background, outputType } = options;
const { width, height, background, outputType, devicePixelRatio = 2 } = options;
const nodeCanvas = createNodeCanvas(width, height, outputType as any);
const offscreenNodeCanvas = createNodeCanvas(1, 1);

Expand All @@ -23,6 +23,7 @@ export function createCanvas(options: Options): [G6Canvas, NodeCanvas] {
// @ts-expect-error missing types
canvas: nodeCanvas as any,
offscreenCanvas: offscreenNodeCanvas as any,
devicePixelRatio,
enableMultiLayer: false,
renderer: () => {
const renderer = new Renderer();
Expand Down

0 comments on commit 94b2ac3

Please sign in to comment.