Skip to main content
Solved

Object oriented programming


Aleksandr16

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

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};
	}
}
View original
This topic has been closed for replies.

2 replies

tank666
  • 4873 replies
  • Answer
  • 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};
	}
}

Aleksandr16

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings