Skip to content

Visualize Your Ideas With Code

feat(2d): always clip images and videos (#773)

Jacob3938c59394bf
diff --git a/packages/2d/src/components/Img.ts b/packages/2d/src/components/Img.ts
index 63aa2bac..4aa1b9c0 100644
--- a/packages/2d/src/components/Img.ts
+++ b/packages/2d/src/components/Img.ts
@@ -43,13 +43,12 @@ export interface ImgProps extends RectProps {
*
* export default makeScene2D(function* (view) {
* const ref = createRef<Img>();
- * view.add(
+ * yield view.add(
* <Img
* ref={ref}
* src="https://images.unsplash.com/photo-1679218407381-a6f1660d60e9"
* width={300}
* radius={20}
- * clip
* />,
* );
*
@@ -194,13 +193,11 @@ export class Img extends Rect {
protected override draw(context: CanvasRenderingContext2D) {
this.drawShape(context);
- if (this.clip()) {
- context.clip(this.getPath());
- }
const alpha = this.alpha();
if (alpha > 0) {
const box = BBox.fromSizeCentered(this.computedSize());
context.save();
+ context.clip(this.getPath());
if (alpha < 1) {
context.globalAlpha *= alpha;
}
@@ -208,6 +205,11 @@ export class Img extends Rect {
drawImage(context, this.image(), box);
context.restore();
}
+
+ if (this.clip()) {
+ context.clip(this.getPath());
+ }
+
this.drawChildren(context);
}
diff --git a/packages/2d/src/components/Video.ts b/packages/2d/src/components/Video.ts
index 6a43e07b..05ffb9f8 100644
--- a/packages/2d/src/components/Video.ts
+++ b/packages/2d/src/components/Video.ts
@@ -209,10 +209,6 @@ export class Video extends Rect {
protected override draw(context: CanvasRenderingContext2D) {
this.drawShape(context);
- if (this.clip()) {
- context.clip(this.getPath());
- }
-
const alpha = this.alpha();
if (alpha > 0) {
const playbackState = this.view().playbackState();
@@ -224,6 +220,7 @@ export class Video extends Rect {
const box = BBox.fromSizeCentered(this.computedSize());
context.save();
+ context.clip(this.getPath());
if (alpha < 1) {
context.globalAlpha *= alpha;
}
@@ -232,6 +229,10 @@ export class Video extends Rect {
context.restore();
}
+ if (this.clip()) {
+ context.clip(this.getPath());
+ }
+
this.drawChildren(context);
}