Skip to content

feat: optionally snap grid items to whole pixels #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions projects/angular-grid-layout/src/lib/grid.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ function getGridHeight(layout: KtdGridLayout, rowHeight: number, gap: number): n
}

// eslint-disable-next-line @katoid/prefix-exported-code
export function parseRenderItemToPixels(renderItem: KtdGridItemRenderData<number>): KtdGridItemRenderData<string> {
export function parseRenderItemToPixels(renderItem: KtdGridItemRenderData<number>, snapToPixels = false): KtdGridItemRenderData<string> {
const top = snapToPixels ? Math.round(renderItem.top) : renderItem.top;
const left = snapToPixels ? Math.round(renderItem.left) : renderItem.left;
return {
id: renderItem.id,
top: `${renderItem.top}px`,
left: `${renderItem.left}px`,
top: `${top}px`,
left: `${left}px`,
width: `${renderItem.width}px`,
height: `${renderItem.height}px`
};
Expand Down Expand Up @@ -251,6 +253,17 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
private _height: number | null = null;
private gridCurrentHeight: number;

/**
* By default, the grid items are positioned based on a computation that may result in
* sub-pixels. The result is in some cases that the content inside the grid items is rendered a bit blurry.
* By setting this parameter to `true` we make sure grid items are positioned on whole pixels.
* Defaults to `false`
*/
@Input() set snapToPixels(val: boolean) {
this._snapToPixels = val;
}
private _snapToPixels:boolean = false;

get config(): KtdGridCfg {
return {
cols: this.cols,
Expand Down Expand Up @@ -357,7 +370,7 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
if (gridItemRenderData == null) {
console.error(`Couldn\'t find the specified grid item for the id: ${item.id}`);
} else {
item.setStyles(parseRenderItemToPixels(gridItemRenderData));
item.setStyles(parseRenderItemToPixels(gridItemRenderData, this._snapToPixels));
}
});
}
Expand Down Expand Up @@ -491,7 +504,7 @@ export class KtdGridComponent implements OnChanges, AfterContentInit, AfterConte
}, gridElemClientRect.width, gridElemClientRect.height);

const newGridItemRenderData = {...this._gridItemsRenderData[gridItem.id]}
const placeholderStyles = parseRenderItemToPixels(newGridItemRenderData);
const placeholderStyles = parseRenderItemToPixels(newGridItemRenderData, this._snapToPixels);

// Put the real final position to the placeholder element
this.placeholder!.style.width = placeholderStyles.width;
Expand Down