All Projects → agilie → canvas-image-cover-position

agilie / canvas-image-cover-position

Licence: MIT License
Calculating image position for scaling it on the canvas.

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to canvas-image-cover-position

lottery-rotate
适配移动端rem布局的canvas大转盘抽奖插件
Stars: ✭ 15 (-60.53%)
Mutual labels:  canvas
okeevis-render
a fast lightweight 2d graphic library
Stars: ✭ 22 (-42.11%)
Mutual labels:  canvas
d3-canvas-transition
transition on canvas with d3
Stars: ✭ 19 (-50%)
Mutual labels:  canvas
img-clipping
基于Canvas的一个h5移动端图片裁剪demo
Stars: ✭ 81 (+113.16%)
Mutual labels:  canvas
huozi.js
A simple typography engine for CJK languages, especially designed for game rich-text. 用于游戏富文本的中日韩文字排印引擎。
Stars: ✭ 135 (+255.26%)
Mutual labels:  canvas
awesome-canvas
Canvas资源库大全中文版。An awesome Canvas packages and resources.
Stars: ✭ 288 (+657.89%)
Mutual labels:  canvas
empathy-map
チームでお互いのことを知り、ゴールや目的を共有する。
Stars: ✭ 15 (-60.53%)
Mutual labels:  canvas
ux-charts
Simple, responsive, modern Charts with zero dependencies
Stars: ✭ 22 (-42.11%)
Mutual labels:  canvas
TakingImageOfAView
An example on how to take screenshot of a particular view
Stars: ✭ 15 (-60.53%)
Mutual labels:  canvas
react-starfield-animation
✨ Canvas-based starfield animation for React.
Stars: ✭ 82 (+115.79%)
Mutual labels:  canvas
vueShop
🙂collections
Stars: ✭ 72 (+89.47%)
Mutual labels:  canvas
maze-generator
A real-time JavaScript maze generator using the depth-first search algorithm
Stars: ✭ 13 (-65.79%)
Mutual labels:  canvas
canvas-td
A fully featured tower defense game built with <canvas>.
Stars: ✭ 31 (-18.42%)
Mutual labels:  canvas
AndroidZdog
Porting Zdog(Round, flat, designer-friendly pseudo-3D engine for canvas) to Android with kotlin
Stars: ✭ 22 (-42.11%)
Mutual labels:  canvas
pikaso
Seamless and headless HTML5 Canvas library
Stars: ✭ 23 (-39.47%)
Mutual labels:  canvas
turkeyvisited
Mark the cities you have visited in Turkey and share the map!
Stars: ✭ 82 (+115.79%)
Mutual labels:  canvas
JustDraw
A Test for Android Canvas Painting with JavaScript Engine
Stars: ✭ 42 (+10.53%)
Mutual labels:  canvas
Ananas
An easy image editor integration for your Android apps.
Stars: ✭ 186 (+389.47%)
Mutual labels:  canvas
js-confetti
JS Confetti library that supports emojis 🦄 🎉 ⚡️
Stars: ✭ 343 (+802.63%)
Mutual labels:  canvas
city-tour
A procedurally generated city built with WebGL and three.js
Stars: ✭ 57 (+50%)
Mutual labels:  canvas

License

Calculate cover size

This method is helpful for calculating size and position of one element to fill another element. A behavior is the same as css property 'background-size: cover;'

Preview Preview

interface CoverSize = {
    width: number;
    height: number;
    offsetLeft: number;
    offsetTop: number;
}

type getCoverSize = (
    contentWidth: number,
    contentHeight: number,
    containerWidth: number,
    containerHeight: number,
    offsetLeft: number = 0.5, 
    offsetTop: number = 0.5) => CoverSize

Offsets values are in range between 0 an 1

Installation

Just add file or function to your project

<script src="https://unpkg.com/@agilie/canvas-image-cover-position"></script>

or

$ npm install @agilie/canvas-image-cover-position

How to use

import getCoverSize from '@agilie/canvas-image-cover-position'

var imageCoverSize = getCoverSize(
    image.naturalWidth,
    image.naturalHeight,
    canvas.width,
    canvas.height,
    0.5,
    0.5
);
canvas.getContext('2d').drawImage(
    image,
    imageCoverSize.offsetLeft,
    imageCoverSize.offsetTop,
    imageCoverSize.width,
    imageCoverSize.height
);

demo

Troubleshooting

Problems? Check the Issues block to find the solution or create an new issue that we will fix asap. Feel free to contribute.

Author

This method is open-sourced by Agilie Team [email protected]

Contributors

Agilie Team

Contact us

If you have any questions, suggestions or just need a help with web or mobile development, please email us at [email protected]. You can ask us anything from basic to complex questions.

We will continue publishing new open-source projects. Stay with us, more updates will follow!

License

The MIT License (MIT) Copyright © 2019 Agilie Team

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].