Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

put image in canvas with cover mode

/**
 * By Ken Fyrstenberg Nilsen
 *
 * drawImageProp(context, image [, x, y, width, height [,offsetX, offsetY]])
 *
 * If image and context are only arguments rectangle will equal canvas
*/
function drawImageProp(ctx, img, x, y, w, h, offsetX, offsetY) {

    if (arguments.length === 2) {
        x = y = 0;
        w = ctx.canvas.width;
        h = ctx.canvas.height;
    }

    // default offset is center
    offsetX = typeof offsetX === "number" ? offsetX : 0.5;
    offsetY = typeof offsetY === "number" ? offsetY : 0.5;

    // keep bounds [0.0, 1.0]
    if (offsetX < 0) offsetX = 0;
    if (offsetY < 0) offsetY = 0;
    if (offsetX > 1) offsetX = 1;
    if (offsetY > 1) offsetY = 1;

    var iw = img.width,
        ih = img.height,
        r = Math.min(w / iw, h / ih),
        nw = iw * r,   // new prop. width
        nh = ih * r,   // new prop. height
        cx, cy, cw, ch, ar = 1;

    // decide which gap to fill    
    if (nw < w) ar = w / nw;                             
    if (Math.abs(ar - 1) < 1e-14 && nh < h) ar = h / nh;  // updated
    nw *= ar;
    nh *= ar;

    // calc source rectangle
    cw = iw / (nw / w);
    ch = ih / (nh / h);

    cx = (iw - cw) * offsetX;
    cy = (ih - ch) * offsetY;

    // make sure source rectangle is valid
    if (cx < 0) cx = 0;
    if (cy < 0) cy = 0;
    if (cw > iw) cw = iw;
    if (ch > ih) ch = ih;

    // fill image in dest. rectangle
    ctx.drawImage(img, cx, cy, cw, ch,  x, y, w, h);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to stop overlapping divs to interact with each others click events 
Javascript :: on close tab react native web 
Javascript :: create multiple buttons in javascript 
Javascript :: bubbling and capturing in javascript 
Javascript :: react composition 
Javascript :: live server in javascript 
Javascript :: call function javascript from asp net 
Javascript :: factorial program in javascript 
Javascript :: indexof js 
Javascript :: could not decode base64 cloudinary 
Javascript :: react native update app from play store ios app store 
Javascript :: Animated Sticky Header 
Javascript :: sprintf js 
Javascript :: freecodecamp cdn 
Javascript :: link external file by url using javascript 
Javascript :: react 18 double render 
Javascript :: how to add class in javascript dynamically 
Javascript :: regex number 
Javascript :: javascript number to string 
Javascript :: map and reduce an array in js 
Javascript :: write files in Node.js 
Javascript :: custom event example 
Javascript :: How to get random no. without math.random() function 
Javascript :: == vs === in javascript 
Javascript :: react hook state not updating immediately 
Javascript :: remove string from outside array javascript 
Javascript :: fullcalendar edit event modal react 
Javascript :: vuejs list items from axios 
Javascript :: read string using stream nodejs 
Javascript :: react useref hook 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =