【问题标题】:issue fabric js applying background color for shapes and background angular问题织物 js 为形状和背景角度应用背景颜色
【发布时间】:2021-06-05 16:33:48
【问题描述】:

我在fabricjs中遇到一个问题,问题是

  1. 我的当前代码具有默认背景,我可以更改背景颜色
  2. 如果我放置任何对象或文本,如果我想将该对象的背景颜色更改为透明度
  3. 我正在尝试更改形状颜色,但背景颜色正在改变。
  4. 实际上我在下面的 html 中发现了问题,我有 props.canvasFill 我认为应该有两个参数,因为如果你看到我的 ts 代码

这是我的 HTML

**<input type="text" class="form-control" [cpPosition]="'right'" [cpPresetColors]="customColors" [cpOKButton]="true" [cpAddColorButtonText]="'Speichern'" [cpAddColorButton]="true" [cpOKButtonClass]="'btn btn-light btn-sm'" [cpSaveClickOutside]="false" [(colorPicker)]="props.canvasFill"
           [style.background]="props.canvasFill" [value]="props.canvasFill" (cpPresetColorsChange)="updateColorPresets($event)"
           (colorPickerChange)="setFillColor($event)"/>**

这是我在 html 中的 ts 代码,目前它有 canvasFile 我必须使用 fill 和 canvasfill 我不知道如何使用它

 props: any = {
    canvasFill: '#ffffff',
    canvasImage: '',
    id: null,
    opacity: null,
    fill: null,
    stroke: null,
    strokeWidth: null,
    fontSize: null,
    lineHeight: null,
    charSpacing: null,
    fontWeight: null,
    fontStyle: null,
    textAlign: null,
    fontFamily: 'Open Sans',
    TextDecoration: '',
    scale: 1,
    angle: 0
};

    setFillColor(swatch: any): void {
        this.palettes.selected = swatch;
        this.props.fill = swatch.key;
        console.log(this.props.fill, '& ' ,swatch.key);
        this.setFill();
        if (!this.props.canvasImage) {
            console.log('say hi')
            this.canvas.backgroundColor = this.props.canvasFill;
            this.canvas.renderAll(); 
        };
    }

console.log(this.props.fill, '& ' ,swatch.key); i am getting Undefined & undefined 

【问题讨论】:

  • 你得到的swatch 变量的值是多少?
  • 我越来越不确定和未定义
  • 不是 swatch.key.. 只是 swatch 变量。在您的方法中将第一行写为console.log(swatch)。控制台中打印的内容
  • @NareshJ 获取颜色代码 setFillColor(swatch: any): void { console.log(swatch);
  • #d82727 这样的颜色代码

标签: angular angular8 fabricjs angular11


【解决方案1】:

swatch 是您获得的用户选择的实际颜色代码,它是字符串。

因此,当您读取swatch 的属性时,您会得到undefined,它是字符串并且字符串没有这样的key 属性。

因此,将this.props.fill = swatch.key; 行替换为this.props.fill = swatch;,您不需要第一行this.palettes.selected = swatch;,除非您在其他地方引用它。

【讨论】:

  • this.props.fill = swatch.key; this.props.fill = 样本; this.setFill(); if (!this.props.canvasImage) { this.canvas.backgroundColor = this.props.canvasFill; this.canvas.renderAll(); };这是正确的
  • 使用当前代码,背景颜色发生变化,当我选择形状时,我正在应用新颜色初始它正在应用背景,当我释放它正在应用形状时
  • &lt;input type="text" class="form-control" [cpPosition]="'right'" [cpPresetColors]="customColors" [cpOKButton]="false" [cpAddColorButtonText]="'Speichern'" [cpAddColorButton]="true" [cpOKButtonClass]="'btn btn-light btn-sm'" [cpSaveClickOutside]="false" [(colorPicker)]="props.canvasFill" [style.background]="props.canvasFill" [value]="props.canvasFill" (cpPresetColorsChange)="updateColorPresets($event)" (colorPickerChange)="setFillColor($event)"/&gt; 这是我的html
  • 你能帮帮我吗
  • 你能帮我解决这个问题吗stackoverflow.com/questions/66588866/…
猜你喜欢
  • 2016-07-09
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多