Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="laptop points">
    <div class="frame">
        <img id="input" src="https://jcubic.pl/laptop.jpg" alt="Laptop"/>
        <p class="image-source">Image taken from <a href="https://pixabay.com/pl/photos/laptop-wideo-netflix-online-5335884/">Pixabay</a></p>
    </div>
    <div id="term"></div>
    <div class="point point-1"></div>
    <div class="point point-2"></div>
    <div class="point point-3"></div>
    <div class="point point-4"></div>
</div>
<div class="open">
    <button id="open">&gt;&gt;&gt;</button>
    <button id="close">&lt;&lt;&lt;</button>
</div>
<aside class="output">
    <p>Drag points to change 3D transformation to make terminal fit the image</p>
    <div class="file">
        <label for="file">
            URL of the image file
        </label>
        <input id="file" value="https://jcubic.pl/laptop.jpg"/>
    </div>
    <div>
        <label for="points">
            Show points
        </label>
        <input id="points" type="checkbox" checked/>
    </div>
    <h3>CSS Output</h3>
    <pre></pre>
</aside>
<!-- questions:

https://graphicdesign.stackexchange.com/questions/86512/find-image-object-perspective-and-rotation/153695#153695

https://stackoverflow.com/questions/59925042/how-to-use-css-perspective-to-match-a-div-to-visually-fit-inside-a-svg-laptop-sc/69085757#69085757

https://stackoverflow.com/questions/36050537/how-to-match-3d-perspective-of-real-photo-and-object-in-css3-3d-transforms/36217808#36217808

-->

              
            
!

CSS

              
                body {
    margin: 0;
}
body.drag * {
    user-select: none;
}
.laptop {
    position: relative;
    width: 900px;
    margin: auto;
}
.laptop .frame,
.laptop img {
    width: 100%;
}
.laptop .frame {
    position: absolute;
}
.image-source {
    text-align: right;
}
.file label, .file input {
    display: block;
}
.file input {
    width: 100%;
    font-size: 1.2em;
    pading: 5px 10px;
    box-sizing: border-box;
}
#term.tranform {
    --transform: matrix3d(1.246140835993741,-0.0669740915373802,0,0.00007956445146971814,0.13757679712421983,1.24518992290358,0,0.000001200713789032468,0,0,1,0,166,172,0,1);
}
#term {
    --size: 0.7;
    position: absolute;
    width: 357px;
    transform: var(--transform);
    transform-origin: 0 0;
    height: 233px;
}
.point {
    position: absolute;
    margin-left: -5px;
    margin-top: -5px;
    width: 10px;
    height: 10px;
    background: blue;
    border-radius: 50%;
    z-index: 100;
    cursor: pointer;
}
.output h2 {
    margin: 0;
}
.output {
    padding: 20px;
    position: fixed;
    top: 0;
    left: 0;
    width: 400px;
    z-index: 200;
    box-sizing: border-box;
    max-height: 100vh;
    overflow-y: auto;
}
.output pre {
    white-space: pre-wrap;
    word-break: break-word;
}
.laptop:not(.points) .point {
    display: none;
} 
.point-1 {
   left: 166px;
   top: 172px;
}
.point-2 {
   left: 594px;
   top: 144px;
}
.point-3 {
   left: 625px;
   top: 426px;
}
.point-4 {
   left: 198px;
   top: 462px;
}
aside.output {
    transition: 1s transform linear;
    background: white;
}
.open {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 400;
}
.open button {
    display: none;
}
@media screen and (max-width: 1400px) {
    body:not(.sidebar) aside.output {
        transform: translateX(-100%);
    }
    body.sidebar #close {
        display: block;
    }
    body:not(.sidebar) #open {
        display: block;
    }
}

              
            
!

JS

              
                const term = $('#term').terminal(function(command) {
    command = command.trim()
    if (!command) {
        return;
    }
    var cmd = $.terminal.split_command(command);
    if (cmd.name === 'echo') {
        var msg = $.terminal.escape_formatting(cmd.rest);
        this.echo(`[[;white;blue]${msg}]`);
    } else {
        this.error('Invalid command');
    }
}, {
    enabled: false,
    name: '3d',
    exit: false
});

const { terminal: geometry } = term.geometry();

term.addClass('tranform');

/*
 * calculation of transform matrix taken from:
 * https://stackoverflow.com/a/36217808/387194
 */
// Computes the matrix3d that maps src points to dst.
function compute_transform(src, dst) {
  // src and dst should have length 4 each
  var count = 4;
  var a = []; // (2*count) x 8 matrix
  var b = []; // (2*count) vector

  for (var i = 0; i < 2 * count; ++i) {
    a.push([0, 0, 0, 0, 0, 0, 0, 0]);
    b.push(0);
  }

  for (var i = 0; i < count; ++i) {
    var j = i + count;
    a[i][0] = a[j][3] = src[i][0];
    a[i][1] = a[j][4] = src[i][1];
    a[i][2] = a[j][5] = 1;
    a[i][3] = a[i][4] = a[i][5] =
      a[j][0] = a[j][1] = a[j][2] = 0;
    a[i][6] = -src[i][0] * dst[i][0];
    a[i][7] = -src[i][1] * dst[i][0];
    a[j][6] = -src[i][0] * dst[i][1];
    a[j][7] = -src[i][1] * dst[i][1];
    b[i] = dst[i][0];
    b[j] = dst[i][1];
  }

  var x = numeric.solve(a, b);
  // matrix3d is homogenous coords in column major!
  // the z coordinate is unused
  var m = [
    x[0], x[3], 0, x[6],
    x[1], x[4], 0, x[7],
    0, 0, 1, 0,
    x[2], x[5], 0, 1
  ];
  return "matrix3d(" + m.join(',') + ')';
}

// Collect the four corners by user clicking in the corners:

var points = [];
// map flatten the array
$('.point').each(function() {
    var {left, top} = $(this).position();
    points.push([left, top]);
});

transform_terminal();
const $body = $('body');
$('.point').each(function(i) {
    var drag = false;
    var [container] = $('.laptop');
    var $point = $(this).mousedown(function() {
        drag = true;
        $body.addClass('drag');
    });
    $(document).on('mouseup', function() {
        drag = false;
        $body.removeClass('drag');
    }).on('mousemove', function(event) {
        if (drag) {
            var box = container.getBoundingClientRect();
            var x = event.clientX - box.left;
            var y = event.clientY - box.top;
            points[i] = [x, y];
            $point.css({
                left: x,
                top: y
            });
            transform_terminal();
        }
    });
});

function transform_terminal() {
    var w = geometry.width + 20,
        h = geometry.height + 20;
    var transform = compute_transform(
        [
            [0, 0],
            [w, 0],
            [w, h],
            [0, h]
        ],
        points
    );
    function format(klass, left, top) {
        return `
.${klass} {
   left: ${left}px;
   top: ${top}px;
}`.trim();
    }
    const point_css = $('.point').map(function() {
        var {left, top} = $(this).position();
        var klas = $(this).attr('class').replace(/point /, ''); 
        return format(klas, left, top);
    }).get().join('\n');
    $('.output pre').html(`
.terminal {
    transform: ${transform};
}
/* optional control points */
${point_css}`.trim())
    term.css({
        '--transform': transform
    });
}
function debounce(func, timeout = 300){
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
        func.apply(this, args);
    }, timeout);
  };
}

$('#file').on('input', debounce(function() {
    input.src = file.value;
}));
$('#points').on('change', function() {
    $('.laptop').toggleClass('points');
});
$('#open').click(function() {
    document.body.classList.add('sidebar');
});
$('#close').click(function() {
    document.body.classList.remove('sidebar');
});
              
            
!
999px

Console