最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - FabricImage class with fromUrl is not working - Stack Overflow

programmeradmin3浏览0评论
import {Component, OnInit} from '@angular/core';
import {Canvas, FabricImage, Image, Rect} from 'fabric';

@Component({
  selector: 'app-shirt',
  imports: [],
  templateUrl: './shirtponent.html',
  styleUrl: './shirtponent.scss'
})

export class ShirtComponent implements OnInit {
  canvas: Canvas | undefined;

  ngOnInit(): void {
    this.canvas = new Canvas('canvas', {
      width: window.innerWidth,
      height: window.innerHeight
    });

    // Add a rectangle to the canvas
    const rectangle = new Rect({
      left: 100,
      top: 100,
      fill: 'blue',
      width: 200,
      height: 100,
    });
    this.canvas.add(rectangle);

    // Load an external image into the canvas
    FabricImage.fromURL('images/custom/shirts/final/1.webp', function (img:any){
      var img1 = img.set({
        left: 300,
        top: 100,
        scaleX: 1.5,
        scaleY: 1.5,
      });
      this.canvas.add(img1);
    });

    this.canvas.renderAll()
  }
}

Angular Version: 19 Fabric.js Version: 6

Angular error

TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. [plugin angular-compiler]

src/app/custom/products/shirt/shirtponent.ts:38:6:
  38 │       this.canvas.add(img1);
     ╵       ~~~~

An outer value of 'this' is shadowed by this container.

src/app/custom/products/shirt/shirtponent.ts:31:61:
  31 │ ...e.fromURL('images/custom/shirts/final/1.webp', function (img:any){

I also use arrow function in the fromUrl but same problem.

What is the actual solution for this.

import {Component, OnInit} from '@angular/core';
import {Canvas, FabricImage, Image, Rect} from 'fabric';

@Component({
  selector: 'app-shirt',
  imports: [],
  templateUrl: './shirtponent.html',
  styleUrl: './shirtponent.scss'
})

export class ShirtComponent implements OnInit {
  canvas: Canvas | undefined;

  ngOnInit(): void {
    this.canvas = new Canvas('canvas', {
      width: window.innerWidth,
      height: window.innerHeight
    });

    // Add a rectangle to the canvas
    const rectangle = new Rect({
      left: 100,
      top: 100,
      fill: 'blue',
      width: 200,
      height: 100,
    });
    this.canvas.add(rectangle);

    // Load an external image into the canvas
    FabricImage.fromURL('images/custom/shirts/final/1.webp', function (img:any){
      var img1 = img.set({
        left: 300,
        top: 100,
        scaleX: 1.5,
        scaleY: 1.5,
      });
      this.canvas.add(img1);
    });

    this.canvas.renderAll()
  }
}

Angular Version: 19 Fabric.js Version: 6

Angular error

TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. [plugin angular-compiler]

src/app/custom/products/shirt/shirtponent.ts:38:6:
  38 │       this.canvas.add(img1);
     ╵       ~~~~

An outer value of 'this' is shadowed by this container.

src/app/custom/products/shirt/shirtponent.ts:31:61:
  31 │ ...e.fromURL('images/custom/shirts/final/1.webp', function (img:any){

I also use arrow function in the fromUrl but same problem.

What is the actual solution for this.

Share Improve this question edited Feb 17 at 6:19 DarkBee 15.6k8 gold badges72 silver badges116 bronze badges asked Feb 17 at 4:59 Mamunur RashidMamunur Rashid 211 silver badge1 bronze badge New contributor Mamunur Rashid is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

Please convert the function which has its own (this) inside this canvas does not exist, instead use an arrow function which takes the scope of the parent, where canvas exists.

    // Load an external image into the canvas
    FabricImage.fromURL('images/custom/shirts/final/1.webp', (img:any) => {
      var img1 = img.set({
        left: 300,
        top: 100,
        scaleX: 1.5,
        scaleY: 1.5,
      });
      this.canvas.add(img1);
    });
发布评论

评论列表(0)

  1. 暂无评论