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

求助:文本渐变色无法起作用 #1572

Closed
alphabetabc opened this issue Nov 1, 2023 · 4 comments
Closed

求助:文本渐变色无法起作用 #1572

alphabetabc opened this issue Nov 1, 2023 · 4 comments
Assignees
Labels

Comments

@alphabetabc
Copy link

alphabetabc commented Nov 1, 2023

代码如下

const text = new Text({
    style: {
        text: `两种渐变都有一个叫做 gradientUnits(渐变单元)的属性,它描述了用来描述渐变的大小和方向的单元系统。该属性有两个值:userSpaceOnUse 、objectBoundingBox。默认值为 objectBoundingBox,我们目前看到的效果都是在这种系统下的,它大体上定义了对象的渐变大小范围,所以你只要指定从 0 到 1 的坐标值,渐变就会自动的缩放到对象相同大小。userSpaceOnUse 使用绝对单元,所以你必须知道对象的位置,并将渐变放在同样地位置上。在每个.shp,.shx与.dbf文件之中,图形在每个文件的排序是一致的。也就是说,.shp的第一条记录与.shx及.dbf之中的第一条记录相对应,如此类推。此外,在.shp与.shx之中,有许多字段的字节序是不一样的。因此用户在编写读取这些文件格式的程序时,必须十分小心地处理不同文件的不同字节序。`,
        x: 0,
        dy: 10,
        textBaseline: 'top',
        wordWrap: true,
        wordWrapWidth: 800,
        fill: 'linear-gradient(90deg, red 0%, green 100%)',
    },
});

await canvas.ready;
canvas.appendChild(text);

下面的效果是换成svg之后的效果
image

@alphabetabc
Copy link
Author

textBaseline: 'bottom' 的时候 渐变色也不好用,有点摸不准这块了

@xiaoiver xiaoiver self-assigned this Nov 3, 2023
@xiaoiver xiaoiver added the bug label Nov 3, 2023
@alphabetabc
Copy link
Author

升级到最新版的G,依然有问题

import { Circle, Canvas, Text, Group } from "@antv/g";
// import { Renderer } from "@antv/g-canvas";
import { Renderer } from "@antv/g-svg";
// import { Renderer } from "@antv/g-webgl";

/**
 * canvas实现无法使用文字渐变色
 */

// create a canvas
const canvas = new Canvas({
    container: document.getElementById("container"),
    width: 800,
    height: 600,
    renderer: new Renderer(), // select a renderer
});

// create a circle
const circle = new Circle({
    style: {
        cx: 100,
        cy: 100,
        r: 50,
        fill: "linear-gradient(90deg, green, red)",
        stroke: "blue",
        lineWidth: 5,
        cursor: "pointer",
    },
});

const text = new Text({
    style: {
        text: `两种渐变都有一个叫做 gradientUnits(渐变单元)的属性,它描述了用来描述渐变的大小和方向的单元系统。该属性有两个值:userSpaceOnUse 、objectBoundingBox。默认值为 objectBoundingBox,我们目前看到的效果都是在这种系统下的,它大体上定义了对象的渐变大小范围,所以你只要指定从 0 到 1 的坐标值,渐变就会自动的缩放到对象相同大小。userSpaceOnUse 使用绝对单元,所以你必须知道对象的位置,并将渐变放在同样地位置上。在每个.shp,.shx与.dbf文件之中,图形在每个文件的排序是一致的。也就是说,.shp的第一条记录与.shx及.dbf之中的第一条记录相对应,如此类推。此外,在.shp与.shx之中,有许多字段的字节序是不一样的。因此用户在编写读取这些文件格式的程序时,必须十分小心地处理不同文件的不同字节序。`,
        x: 0,
        // dy: 0,
        textBaseline: "top",
        wordWrap: true,
        wordWrapWidth: 800,
        // fill: "linear-gradient(90deg, red 0%, green 100%)",
        fill: "linear-gradient(90deg, green, red)",
        // fill: "red",
        fillOpacity: 1,
    },
});

const g1 = new Group();
g1.append(text);

/**
 * 此处渐变不好使
 */
const text2 = new Text({
    style: {
        text: `两种渐变都有一个叫做 gradientUnits(渐变单元)的属性,它描述了用来描述渐变的大小和方向的单元系统。该属性有两个值:userSpaceOnUse 、objectBoundingBox。默认值为 objectBoundingBox,我们目前看到的效果都是在这种系统下的,它大体上定义了对象的渐变大小范围,所以你只要指定从 0 到 1 的坐标值,渐变就会自动的缩放到对象相同大小。userSpaceOnUse 使用绝对单元,所以你必须知道对象的位置,并将渐变放在同样地位置上。在每个.shp,.shx与.dbf文件之中,图形在每个文件的排序是一致的。也就是说,.shp的第一条记录与.shx及.dbf之中的第一条记录相对应,如此类推。此外,在.shp与.shx之中,有许多字段的字节序是不一样的。因此用户在编写读取这些文件格式的程序时,必须十分小心地处理不同文件的不同字节序。`,
        x: 0,
        y: 250,
        textBaseline: "top",
        wordWrap: true,
        wordWrapWidth: 800,
        fill: "linear-gradient(90deg, green, red)",
        // fill: 'red',
        fillOpacity: 1,
    },
});

console.log(text);

await canvas.ready;

// append to canvas
canvas.appendChild(circle);
canvas.appendChild(g1);
canvas.appendChild(text2);
  • svg 显示有问题
  • canvas 文字都不显示
    image

@xiaoiver
Copy link
Contributor

正确效果如下:

截屏2024-05-23 下午2 53 21

xiaoiver added a commit that referenced this issue May 23, 2024
* fix: render correctly in canvas when stroke is empty #1616

* chore: commit changeset

* fix: support gradient text in canvas & svg #1572
xiaoiver added a commit that referenced this issue May 23, 2024
* fix: support gradient text (#1694)

* fix: render correctly in canvas when stroke is empty #1616

* chore: commit changeset

* fix: support gradient text in canvas & svg #1572

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

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

* chore: transform d3-color in jest

* fix: skip filling on text

---------

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>
@xiaoiver
Copy link
Contributor

xiaoiver commented May 23, 2024

升级到 @antv/g@6.0.7 和 @antv/g-canvas@2.0.6 即可,例子可以参考:https://github.com/antvis/G/blob/next/__tests__/demos/bugfix/1572.ts

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

No branches or pull requests

2 participants