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

He mind #611

merged 4 commits into from
Jun 12, 2024

Conversation

hingq
Copy link
Contributor

@hingq hingq commented Jun 7, 2024

调整使用canvas绘制出现线条模糊的情况
fix the blur of line when using canvas

Copy link
Owner

@hizzgdev hizzgdev left a comment

Choose a reason for hiding this comment

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

感谢 @hingq 的支持,你能否帖两个截图做对比,好更直观的看到这个 PR 的效果?

@@ -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

package.json Outdated
@@ -16,7 +16,7 @@
"scripts": {
"server": "http-server",
"build": "rollup -c .config/rollup.config.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test": " NODE_OPTIONS=--experimental-vm-modules jest",
Copy link
Owner

Choose a reason for hiding this comment

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

这个修改没有必要,可以撤销掉

@@ -1,37 +0,0 @@
# jsMind ES6 Version
Copy link
Owner

Choose a reason for hiding this comment

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

es6里的文件应该不影响这个PR,是否可以保留?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以

@@ -0,0 +1,6 @@
{
Copy link
Owner

Choose a reason for hiding this comment

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

这个是否也可以删除?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以的,json文件里的是我自己的node环境没法运行,不小心改掉的

@hingq
Copy link
Contributor Author

hingq commented Jun 8, 2024

屏幕截图 2024-06-08 091151

@hizzgdev
Copy link
Owner

hizzgdev commented Jun 8, 2024

我也在本地测试了一下,效果非常好!不过好像会影响到截图插件的功能。截图插件用的也是 canvas (无论 engine 是 canvas 还是 svg), 我想它可能也需要使用相同的 dpr 逻辑。

  • 当 engine 为默认值 canvas 时,截图里的线条是 dpr * 的状态
  • 当 engine 为 svg 时,截图里的线条是模糊的。

使用下面的 option 可以测试 svg 或 canvas 不同 engine.

var options = {
    ......
    view: {
        engine: 'canvas', // svg
        draggable: true,
    },
    plugin: {
        screenshot: {
            background: '#ffffff'
        }
    }
};

不知你能否方便一起处理一下,不方便的话等这个 PR 合并后我来弄也 OK。谢谢!

@hingq
Copy link
Contributor Author

hingq commented Jun 8, 2024

我尝试处理一下。还有一个很奇怪的问题,当我使用vue3时,只能显示线条,无法显示对应的节点,这个问题在react里面和html的demo里面却没有遇到

	modified:   src/plugins/jsmind.screenshot.js
	modified:   tests/unit/jsmind.graph.test.js
@hizzgdev
Copy link
Owner

hizzgdev commented Jun 9, 2024

测试了一下最后的这个版本,canvas engin 下截图正常了,不过截图的质量好像并没有改善,我有空再细看一下。

另外我想到 canvas 在各浏览器上是有最大尺寸限制的,在搜索引擎上可以搜到这个问题,之前有用户也提过如果一个脑图非常大的时候,导致线条显示不完整的问题(解决方案是换成 svg),我不确定使用 width * dpr 的方法会不会让实际的 max size 变得更小了,后面可以再测试一下。如果它真的对 max size 有影响的话,我觉得还是可以设置一个类似 high_dpi_enabled 这样的参数,设置为 true 时再获取 dpr, 否则保持 drp=1.

再次感谢你的贡献,我会在稍后的几天里尽快完成验证,可能会做出一些必要的修改。谢谢!

@hizzgdev
Copy link
Owner

hizzgdev commented Jun 9, 2024

我尝试处理一下。还有一个很奇怪的问题,当我使用vue3时,只能显示线条,无法显示对应的节点,这个问题在react里面和html的demo里面却没有遇到

你是说截图的时候吗?我也不是很清楚,怀疑是与异步 dom 渲染有关?或者 dom2Image 不兼容 vue3 生成的dom 的某些属性?

@hingq
Copy link
Contributor Author

hingq commented Jun 9, 2024 via email

@hizzgdev
Copy link
Owner

Hi @hingq 我写了段简单的html验证了一下,如果不使用 hdr, 则在 chrome 浏览器下,可将 canvas 的 width/height 设置到 16000/16000, 如果使用了 hdr (dpr=2),width/height 到 9000 时 canvas 就画不出来了。
image
image

这是测试代码(点击展开)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas test</title>
</head>
<body>

<div style="position: fixed; top:0; left:0;">
<label for="canvasWidth">Canvas size: </label>
<input type="number" id="canvasWidth" value="9000"> * <input type="number" id="canvasHeight" value="9000">
HDR enabled: <input type="checkbox" id="hdr" value="1">
<button id="drawButton">Test</button>
</div>
<div id="canvasContainer" style="margin-top: 40px;"></div>

<script>
function testCanvas(){
    var canvasContainer = document.getElementById('canvasContainer');

    var cw = parseInt(document.getElementById('canvasWidth').value, 10);
    var ch = parseInt(document.getElementById('canvasHeight').value, 10);
    var hdr = document.getElementById('hdr').checked;
    var dpr = hdr?window.devicePixelRatio:1;
    drawPoint(canvasContainer, cw, ch, dpr)
}

function drawPoint(container, cw, ch, dpr){
    container.innerHTML = '';
    var canvas = document.createElement('canvas');
    canvas.width = cw * dpr;
    canvas.height = ch * dpr;
    canvas.style.width = cw+'px';
    canvas.style.height = ch+'px';
    canvasContainer.appendChild(canvas);

    var ctx = canvas.getContext('2d');
    if(dpr!=1){
        ctx.scale(dpr, dpr)
    }

    for (var i = 0; i < cw; i+=100) {
        for (var j = 0; j < ch; j+=100) {
            ctx.fillStyle = 'black';
            ctx.fillRect(i, j, 5, 5);
            ctx.font = '12px Arial';
            ctx.fillText('(' + i + ',' + j + ')', i + 10, j + 10);
        }
    }
}

document.getElementById('drawButton').addEventListener('click', testCanvas);

</script>

</body>
</html>

@hizzgdev
Copy link
Owner

基于这个情况,我会添加一个 option 用于启用 devicePixelRatio, 并且默认不开启,以避免对用户的线上产品造成影响(设想用户的脑图非常大,本来还能正常显示脑图,但升级了 jsMind 之后,突然就不能使用了)。

@hingq
Copy link
Contributor Author

hingq commented Jun 12, 2024 via email

@hizzgdev hizzgdev merged commit b6fe6d6 into hizzgdev:master Jun 12, 2024
3 checks passed
@hizzgdev
Copy link
Owner

代码已合并,待完善文档后发布新版本。再次感谢! @hingq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants