123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html lang = "en"> 
<head>
    <meta charset="UTF-8">
    <title> TestTask </title>
    <link rel = "stylesheet" href="style.css" >
</head>
<body style = "margin: 0">
    <input id = "horisontal" type="button" value="Horisontal"/> 
    <input id = "vertical" type="button" value="Vertical"/> 
    <input id = "clear" type="button" value="Clear"/><br/> <br/>
    <input id = "first" type="file"/> 
    <input id = "second" type="file"/> 
    
    
    <canvas id ="canvas" style = "display: block;"></canvas>    
    <script src = "script.js" charset="utf-8"> </script>  
</body>

</html>




            
          
!
            
              //Use canvas
const canvas = document.querySelector('canvas');
const ctx    = canvas.getContext('2d');

//The size of canvas is size window
canvas.width = 500;
canvas.height = 400;

//variables for a checking image selection
var changeFile = false;
var position = 0;
var width;
var height;


//variables who get the id of buttons
const first      = document.querySelector('input[id="first"]');
const second     = document.querySelector('input[id="second"]');
const horisontal = document.querySelector('input[id ="horisontal"]');
const vertical   = document.querySelector('input[id ="vertical"]');
const clear      = document.querySelector('input[id="clear"]');


vertical.addEventListener('click', function() {
    position++;
    width = 200;
    //event for a first button
    first.addEventListener('change', function() {
        //read the files 
        const reader = new FileReader();
        reader.onload = function () {
            //create the variable for a future image
            const imgOneV = new Image();
            //load the image
            imgOneV.onload = function () {
                //Check if this is the first download of the image
                if(changeFile==false) {                
                    height = (imgOneV.height*width)/imgOneV.width;
                    changeFile = true;
                    ctx.drawImage( imgOneV, 0, 0, imgOneV.width, imgOneV.height, 0, 0, width, height);                    
                }else{
                    ctx.drawImage( imgOneV, 0, 0, imgOneV.width, imgOneV.height, 0, height, width, (imgOneV.height*width)/imgOneV.width);
                }               
            }
            imgOneV.src = reader.result;
        }
        reader.readAsDataURL(first.files[0]);
    }, false);

    //event for a second button
    second.addEventListener('change', function() {
        //read the files 
        const reader = new FileReader();
        reader.onload = function () {
            //create the variable for a future image
            const imgTwoV = new Image();
            //load the image
            imgTwoV.onload = function () {
                //Check if this is the first download of the image
                if(changeFile==false) {
                    height = (imgTwoV.height*width)/imgTwoV.width;
                    changeFile = true;
                    ctx.drawImage( imgTwoV, 0, 0, imgTwoV.width, imgTwoV.height, 0, 0, width, height);          
                }else{
                    ctx.drawImage( imgTwoV, 0, 0, imgTwoV.width, imgTwoV.height, 0, height, width,  (imgTwoV.height*width)/imgTwoV.width);                               
                }
            }
            imgTwoV.src = reader.result;       
        }
        reader.readAsDataURL(second.files[0]);
    }, false);
        
}, false);

horisontal.addEventListener('click', function() {
    position++;
    height = 100;
    //event for a first button
    first.addEventListener('change', function() {
        //read the files 
        const reader = new FileReader();
        reader.onload = function () {
            //create the variable for a future image
            const imgOneH = new Image();
            //load the image
            imgOneH.onload = function () {
                //Check if this is the first download of the image
                if(changeFile==false) {                
                    width = (imgOneH.width*height)/imgOneH.height;
                    changeFile = true;
                    ctx.drawImage( imgOneH, 0, 0, imgOneH.width, imgOneH.height, 0, 0, width, height);                    
                }else{
                    ctx.drawImage( imgOneH, 0, 0, imgOneH.width, imgOneH.height, width, 0, (imgOneH.width*height)/imgOneH.height, height);
                }               
            }
            imgOneH.src = reader.result;
        }
        reader.readAsDataURL(first.files[0]);
    }, false);

    //event for a second button
    second.addEventListener('change', function() {
        //read the files 
        const reader = new FileReader();
        reader.onload = function () {
            //create the variable for a future image
            const imgTwoH = new Image();
            //load the image
            imgTwoH.onload = function () {
                //Check if this is the first download of the image
                if(changeFile==false) {
                    width = (imgTwoH.width*height)/imgTwoH.height;
                    changeFile = true;
                    ctx.drawImage( imgTwoH, 0, 0, imgTwoH.width, imgTwoH.height, 0, 0, width, height);          
                }else{
                    ctx.drawImage( imgTwoH, 0, 0, imgTwoH.width, imgTwoH.height, width, 0, (imgTwoH.width*height)/imgTwoH.height, height);                               
                }
            }
            imgTwoH.src = reader.result;       
        }
        reader.readAsDataURL(second.files[0]);
    }, false);
}, false);

clear.addEventListener('click', function() {
    first.value = "";
    second.value = "";
    ctx.fillStyle = 'white';
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    changeFile=false;    
    
},false);

first.addEventListener('change', function() {
    if(position==0){
        //alert("Please change a position! Now click on clear)")
    }
}, false);


second.addEventListener('change', function() {
    if(position==0){
        //alert("Please change a position! Now click on clear)")
    }
}, false);






            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console