Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

He mind #611

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions es6/README-en.md

This file was deleted.

34 changes: 0 additions & 34 deletions es6/README.md

This file was deleted.

5 changes: 1 addition & 4 deletions example/2_features_cn.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@
<input class="file" type="file" id="image-chooser" accept="image/*" />
</div>
</div>
<script
type="text/javascript"
src="//jsd.onmicrosoft.cn/npm/[email protected]/es6/jsmind.js"
></script>
<script type="text/javascript" src="../es6/jsmind.js"></script>
<script
type="text/javascript"
src="//jsd.onmicrosoft.cn/npm/[email protected]/es6/jsmind.draggable-node.js"
Expand Down
21 changes: 18 additions & 3 deletions src/jsmind.graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class CanvasGraph {
};
this.init_line_render();
}
getPixelRatio() {
return window.devicePixelRatio || 1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

关于 dpr 的选项是否需要放在 option 里,默认值可以是 window.devicePixelRatio || 1 ?这样或许用户还能自定义缩放比例?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

window.devicePixelRatio || 1 会获取像素比,不同的设备像素比不尽相同,为了保证兼容性,后面返回1。不会影响自定义缩放比列。个人认为,可以不用放在option

}
init_line_render() {
if (typeof this.opts.custom_line_render === 'function') {
this.drawing = (ctx, x1, y1, x2, y2) => {
Expand All @@ -144,11 +147,23 @@ class CanvasGraph {
return this.e_canvas;
}
set_size(w, h) {
let dpr = this.getPixelRatio();

this.size.w = w;
this.size.h = h;
this.e_canvas.width = w;
this.e_canvas.height = h;
if (this.e_canvas.width && this.e_canvas.height && this.canvas_ctx.scale) {
this.e_canvas.width = w * dpr;
this.e_canvas.height = h * dpr;

this.e_canvas.style.width = w + 'px';
this.e_canvas.style.height = h + 'px';
this.canvas_ctx.scale(dpr, dpr);
} else {
this.e_canvas.width = w;
this.e_canvas.height = h;
}
}

clear() {
this.canvas_ctx.clearRect(0, 0, this.size.w, this.size.h);
}
Expand All @@ -160,7 +175,7 @@ class CanvasGraph {
this.drawing(ctx, pin.x + offset.x, pin.y + offset.y, pout.x + offset.x, pout.y + offset.y);
}
copy_to(dest_canvas_ctx, callback) {
dest_canvas_ctx.drawImage(this.e_canvas, 0, 0);
dest_canvas_ctx.drawImage(this.e_canvas, 0, 0, this.size.w, this.size.h);
!!callback && callback();
}
_bezier_to(ctx, x1, y1, x2, y2) {
Expand Down
6 changes: 6 additions & 0 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/plugins/jsmind.screenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ class JmScreenshot {
this.version = '0.2.0';
this.jm = jm;
this.options = opts;
this.dpr = window.devicePixelRatio || 1;
}

shoot() {
let c = this.create_canvas();
let ctx = c.getContext('2d');
ctx.scale(this.dpr, this.dpr);
Promise.resolve(ctx)
.then(() => this.draw_background(ctx))
.then(() => this.draw_lines(ctx))
Expand All @@ -53,8 +55,14 @@ class JmScreenshot {

create_canvas() {
let c = $.c('canvas');
c.width = this.jm.view.size.w;
c.height = this.jm.view.size.h;
const w = this.jm.view.size.w;
const h = this.jm.view.size.h;

c.width = w * this.dpr;
c.height = h * this.dpr;
c.style.width = w + 'px';
c.style.height = h + 'px';

c.style.visibility = 'hidden';
this.jm.view.e_panel.appendChild(c);
return c;
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/jsmind.graph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ describe('graph over canvas', () => {
const mockDest = { drawImage: jest.fn() };
const callback = jest.fn();
graph.copy_to(mockDest, callback);
expect(mockDest.drawImage).toBeCalledWith(mockCanvas, 0, 0);
expect(mockDest.drawImage).toBeCalledWith(
mockCanvas,
0,
0,
mockCanvas.width,
mockCanvas.height
);
expect(callback).toBeCalled();
});
});
Expand Down
Loading