Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d55c8be

Browse files
committed
Center images in their bounds when painting
Eventually we'll plumb positionX and positionY out so that developers can control them.
1 parent 1a933a7 commit d55c8be

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

sky/packages/sky/lib/painting/box_painter.dart

+30-29
Original file line numberDiff line numberDiff line change
@@ -236,54 +236,55 @@ void paintImage({
236236
sky.Image image,
237237
sky.ColorFilter colorFilter,
238238
fit: ImageFit.scaleDown,
239-
repeat: ImageRepeat.noRepeat
239+
repeat: ImageRepeat.noRepeat,
240+
double positionX: 0.5,
241+
double positionY: 0.5
240242
}) {
241243
Size bounds = rect.size;
242244
Size imageSize = new Size(image.width.toDouble(), image.height.toDouble());
243-
Size src;
244-
Size dst;
245+
Size sourceSize;
246+
Size destinationSize;
245247
switch(fit) {
246248
case ImageFit.fill:
247-
src = imageSize;
248-
dst = bounds;
249+
sourceSize = imageSize;
250+
destinationSize = bounds;
249251
break;
250252
case ImageFit.contain:
251-
src = imageSize;
252-
if (bounds.width / bounds.height > src.width / src.height) {
253-
dst = new Size(bounds.width, src.height * bounds.width / src.width);
254-
} else {
255-
dst = new Size(src.width * bounds.height / src.height, bounds.height);
256-
}
253+
sourceSize = imageSize;
254+
if (bounds.width / bounds.height > sourceSize.width / sourceSize.height)
255+
destinationSize = new Size(sourceSize.width * bounds.height / sourceSize.height, bounds.height);
256+
else
257+
destinationSize = new Size(bounds.width, sourceSize.height * bounds.width / sourceSize.width);
257258
break;
258259
case ImageFit.cover:
259-
if (bounds.width / bounds.height > imageSize.width / imageSize.height) {
260-
src = new Size(imageSize.width, imageSize.width * bounds.height / bounds.width);
261-
} else {
262-
src = new Size(imageSize.height * bounds.width / bounds.height, imageSize.height);
263-
}
264-
dst = bounds;
260+
if (bounds.width / bounds.height > imageSize.width / imageSize.height)
261+
sourceSize = new Size(imageSize.width, imageSize.width * bounds.height / bounds.width);
262+
else
263+
sourceSize = new Size(imageSize.height * bounds.width / bounds.height, imageSize.height);
264+
destinationSize = bounds;
265265
break;
266266
case ImageFit.none:
267-
src = new Size(math.min(imageSize.width, bounds.width),
268-
math.min(imageSize.height, bounds.height));
269-
dst = src;
267+
sourceSize = new Size(math.min(imageSize.width, bounds.width),
268+
math.min(imageSize.height, bounds.height));
269+
destinationSize = sourceSize;
270270
break;
271271
case ImageFit.scaleDown:
272-
src = imageSize;
273-
dst = bounds;
274-
if (src.height > dst.height) {
275-
dst = new Size(src.width * dst.height / src.height, src.height);
276-
}
277-
if (src.width > dst.width) {
278-
dst = new Size(dst.width, src.height * dst.width / src.width);
279-
}
272+
sourceSize = imageSize;
273+
destinationSize = bounds;
274+
if (sourceSize.height > destinationSize.height)
275+
destinationSize = new Size(sourceSize.width * destinationSize.height / sourceSize.height, sourceSize.height);
276+
if (sourceSize.width > destinationSize.width)
277+
destinationSize = new Size(destinationSize.width, sourceSize.height * destinationSize.width / sourceSize.width);
280278
break;
281279
}
282280
// TODO(abarth): Implement |repeat|.
283281
Paint paint = new Paint();
284282
if (colorFilter != null)
285283
paint.setColorFilter(colorFilter);
286-
canvas.drawImageRect(image, Point.origin & src, rect.topLeft & dst, paint);
284+
double dx = (bounds.width - destinationSize.width) * positionX;
285+
double dy = (bounds.height - destinationSize.height) * positionY;
286+
Point destinationPosition = rect.topLeft + new Offset(dx, dy);
287+
canvas.drawImageRect(image, Point.origin & sourceSize, destinationPosition & destinationSize, paint);
287288
}
288289

289290
typedef void BackgroundImageChangeListener();

0 commit comments

Comments
 (0)