JSPM

element-functions-set_background_canvas

1.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q35034F
  • License MIT

Package Exports

  • element-functions-set_background_canvas

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (element-functions-set_background_canvas) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

setBackgroundCanvas

Sets an Image as the background of any Element using a single function.

This functions allows you to apply an image behind all element children. By using this function you will have far more control over your image. The main reason for this was to allow opacity over an still background image without effecting nested elements. Now each nested element can have its own alpha use setAlpha project for this. Use the opacity rule for the canvas.

Image Example

        /* create the image */
        var img1 = new Image();

    //drawing of the test image - img1
    img1.onload = function () {
        //draw background image
        
        /* run the function with the object */
        document.getElementById("your_block_level_element").setBackgroundCanvas(img1);

    };

    img1.src = 'https://upload.wikimedia.org/wikipedia/commons/9/9b/The.Matrix.glmatrix.2.png';

Video Example

            var vid1 = document.createElement("video");
            vid1.id = "vid_1";
            vid1.src = "http://your_video_url.com/video.mp4";
            vid1.muted = true; 
            var vw,vh;
            vid1.addEventListener("loadedmetadata", function() {
                document.getElementById("your_block_level_element").setBackgroundCanvas(this);
            /* you can use a timer for a delay */
                    setTimeout(function(){
                        vid1.play();
                    }, 5000)

        }, false);

BackgroundCanvasLoaded

Event


document.getElementById("your_block_level_element").addEventListener("BackgroundCanvasLoaded", function(event){
           // apply the settings 
           event.target.getBackgroundCanvas().classList.add("x-x","y-y");

           // set the opacity if you wish
           event.target.getBackgroundCanvas().style.opacity = 0.5;

           event.target.reFresh();
});

Requires

containsAll https://github.com/webciter/containsAll
arrayMin https://github.com/webciter/arrayMin

Installation

npm i element-functions-set_background_canvas

Positioning

To reposition the image use two classes on the canvas element, both x and y classes are needed.

9 positions are possible

 xl yt, xc yt, xr yt
 xl yc, xc yc, xr yc
 xl yb, xc yb, xr yb
 

So for a center image use

/* center image */
 document.getElementById("your_block_level_element").getBackgroundCanvas().classList.add("xc yc");
 

Stretch

The object ether image, video can be stretched over the element, using the following classes. aspect ratio is ignored.

 x-x - x axis only
 y-y - y axis only
 xy-xy - both axis
 

Opacity

Use the opacity rule, inheritance will not be effected as it's on it own branch.

  /* 50% Opacity */
  document.getElementById("your_block_level_element").getBackgroundCanvas().style.opacity = 0.5;

 

Repeat

/* only on the x axis */
 document.getElementById("your_block_level_element").getBackgroundCanvas().classList.add("xx");

/* only on the y axis */
 document.getElementById("your_block_level_element").getBackgroundCanvas().classList.add("yy");

/* both axis, essentially tile cover */
 document.getElementById("your_block_level_element").getBackgroundCanvas().classList.add("xx yy");