Skip to main content
Aleksandr16
July 5, 2023
Solved

Object oriented programming

  • July 5, 2023
  • 2 replies
  • 531 views

Hi there. Is it possible to use OOP paradigm in plugin development?

I wish to make objects extends class. And class must have some methods like async method receiving src and method creating rectangle (at picture below). I can’t figure out how to turn such API requests into class methods. Help with example
image

This topic has been closed for replies.
Best answer by tank666

It’s not entirely clear what difficulties you’re facing. Just create a class(es) with the required methods.

For example:

class CreateRectangle {
	createRectangle(imageData) {
		const rect = figma.createRectangle();
		if (imageData) {
			rect.resize(imageData.width, imageData.height);
			rect.fills = [{ type: 'IMAGE', imageHash: imageData.hash, scaleMode: 'FILL' }];
		}
		return rect;
	}
}

class CreateImage extends CreateRectangle {
	async createImageAsync(imageData) {
		const image = await figma.createImageAsync(imageData);
		const { width, height } = await image.getSizeAsync();
		return {hash: image.hash, width, height};
	}
}

2 replies

tank666
tank666Solution
July 5, 2023

It’s not entirely clear what difficulties you’re facing. Just create a class(es) with the required methods.

For example:

class CreateRectangle {
	createRectangle(imageData) {
		const rect = figma.createRectangle();
		if (imageData) {
			rect.resize(imageData.width, imageData.height);
			rect.fills = [{ type: 'IMAGE', imageHash: imageData.hash, scaleMode: 'FILL' }];
		}
		return rect;
	}
}

class CreateImage extends CreateRectangle {
	async createImageAsync(imageData) {
		const image = await figma.createImageAsync(imageData);
		const { width, height } = await image.getSizeAsync();
		return {hash: image.hash, width, height};
	}
}
UX/UI designer. Figma expert. Creator and developer of plugins and widgets for Figma and FigJam. Check out my resources in Figma Community: figma.com/@tank
Aleksandr16
July 5, 2023

This is example i’ve looked for 🤝
Good luck =)