将图像从剪贴板粘贴到 html 页面

Paste image from clipboard to html page

我正在学习 angular,我正在尝试使用剪贴板中的复制图像并将其粘贴到 html 网页中。 我在 Javascript 中找到了一个解决方案,我想将其转换为 typeScript。但是当我测试它时它不起作用。它在 jsfiddle 上运行良好:http://jsfiddle.net/KJW4E/1739/

<!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>

    <script>
    var CLIPBOARD = new CLIPBOARD_CLASS("my_canvas", true);

    /**
     * image pasting into canvas
     * 
     * @param {string} canvas_id - canvas id
     * @param {boolean} autoresize - if canvas will be resized
     */
    function CLIPBOARD_CLASS(canvas_id, autoresize) {
        var _self = this;
        var canvas = document.getElementById(canvas_id);
        var ctx = document.getElementById(canvas_id).getContext("2d");
        var ctrl_pressed = false;
        var command_pressed = false;
        var paste_event_support;
        var pasteCatcher;

        //handlers
        document.addEventListener('keydown', function (e) {
            _self.on_keyboard_action(e);
        }, false); //firefox fix
        document.addEventListener('keyup', function (e) {
            _self.on_keyboardup_action(e);
        }, false); //firefox fix
        document.addEventListener('paste', function (e) {
            _self.paste_auto(e);
        }, false); //official paste handler

        //constructor - we ignore security checks here
        this.init = function () {
            pasteCatcher = document.createElement("div");
            pasteCatcher.setAttribute("id", "paste_ff");
            pasteCatcher.setAttribute("contenteditable", "");
            pasteCatcher.style.cssText = 'opacity:0;position:fixed;top:0px;left:0px;width:10px;margin-left:-20px;';
            document.body.appendChild(pasteCatcher);

            // create an observer instance
            var observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (paste_event_support === true || ctrl_pressed == false || mutation.type != 'childList'){
                        //we already got data in paste_auto()
                        return true;
                    }

                    //if paste handle failed - capture pasted object manually
                    if(mutation.addedNodes.length == 1) {
                        if (mutation.addedNodes[0].src != undefined) {
                            //image
                            _self.paste_createImage(mutation.addedNodes[0].src);
                        }
                        //register cleanup after some time.
                        setTimeout(function () {
                            pasteCatcher.innerHTML = '';
                        }, 20);
                    }
                });
            });
            var target = document.getElementById('paste_ff');
            var config = { attributes: true, childList: true, characterData: true };
            observer.observe(target, config);
        }();
        //default paste action
        this.paste_auto = function (e) {
            paste_event_support = false;
            if(pasteCatcher != undefined){
                pasteCatcher.innerHTML = '';
            }
            if (e.clipboardData) {
                var items = e.clipboardData.items;
                if (items) {
                    paste_event_support = true;
                    //access data directly
                    for (var i = 0; i < items.length; i++) {
                        if (items[i].type.indexOf("image") !== -1) {
                            //image
                            var blob = items[i].getAsFile();
                            var URLObj = window.URL || window.webkitURL;
                            var source = URLObj.createObjectURL(blob);
                            this.paste_createImage(source);
                        }
                    }
                    e.preventDefault();
                }
                else {
                    //wait for DOMSubtreeModified event
                    //https://bugzilla.mozilla.org/show_bug.cgi?id=891247
                }
            }
        };
        //on keyboard press
        this.on_keyboard_action = function (event) {
            k = event.keyCode;
            //ctrl
            if (k == 17 || event.metaKey || event.ctrlKey) {
                if (ctrl_pressed == false)
                    ctrl_pressed = true;
            }
            //v
            if (k == 86) {
                if (document.activeElement != undefined && document.activeElement.type == 'text') {
                    //let user paste into some input
                    return false;
                }

                if (ctrl_pressed == true && pasteCatcher != undefined){
                    pasteCatcher.focus();
                }
            }
        };
        //on kaybord release
        this.on_keyboardup_action = function (event) {
            //ctrl
            if (event.ctrlKey == false && ctrl_pressed == true) {
                ctrl_pressed = false;
            }
            //command
            else if(event.metaKey == false && command_pressed == true){
                command_pressed = false;
                ctrl_pressed = false;
            }
        };
        //draw pasted image to canvas
        this.paste_createImage = function (source) {
            var pastedImage = new Image();
            pastedImage.onload = function () {
                if(autoresize == true){
                    //resize
                    canvas.width = pastedImage.width;
                    canvas.height = pastedImage.height;
                }
                else{
                    //clear canvas
                    ctx.clearRect(0, 0, canvas.width, canvas.height);
                }
                ctx.drawImage(pastedImage, 0, 0);
            };
            pastedImage.src = source;
        };
    }




    </script>

    </head>
    <body>

    1. Copy image data into clipboard or press Print Screen <br>
    2. Press Ctrl+V (page/iframe must be focused):
    <br /><br />
    <button onclick="alert('Hello! I am an alert box!!');"></button>
    <canvas style="border:1px solid grey;" id="my_canvas" width="300" height="300"></canvas>

    </body>
    </html>

我正在使用 google chrome 版本 70.0.3538.77

提前致谢

从上面的评论来看,您似乎试图在组件的 constructor 中而不是在 ngOnInit 中执行绑定。这是一个典型的陷阱,因为构造函数在屏幕上的内容完成渲染之前(在大多数情况下)就已经启动了。 ngOnInit 在组件完成其实例化过程时被调用。

https://angular-6ckpxd.stackblitz.io

您收到的错误是因为打字稿是强类型的,而 javascript 不是。您正在粘贴 javascript 代码(在大多数情况下这很好),但如果您想消除大部分代码,则需要转换为 any