簡體   English   中英

如何根據javascript中的上傳圖像調整畫布大小?

[英]How to resize canvas according to upload image in javascript?

我有以下代碼:

<script type="text/javascript">
//start pdf to canvas

 ...

//end pdf to canvas
var a="123_m";
var imgname = "images/im/"+a+"/2";

var side = 1;
$(document).ready(function () {
    $("#ImageUpload").uploadify({
        'multi': false,
        'queueSizeLimit': 1,
        'fileSizeLimit': 0,
        'progressData': 'speed',
        'swf': 'upscripts/uploadify.swf',
        'width': 67,
        'height': 50,
        'folder': 'Uploads',
        'auto': true,
        'onUploadError': function (file, errorCode, errorMsg, errorString) {
        },
        'onUploadSuccess': function (file, response) {

            var a = file.name;
            var b = "asdfd";
            angular.element("#canvascontainer").scope().InsertImage(a);
        }
    });

    var canvasdiscription = [{ "objects": [], "background": "rgba(0, 0, 0, 0)", "backgroundImage": "pug.jpg", "backgroundImageOpacity": 1, "backgroundImageStretch": true}];
    canvasdiscription[0].objects.push({ "type": "leg-text", "left": 202, "top": 51, "width": 231, "height": 31.2, "fill": "#000000", "overlayFill": null, "stroke": null, "strokeWidth": 1, "strokeDashArray": null, "scaleX": 1.53, "scaleY": 1.53, "angle": 0, "flipX": false, "flipY": false, "opacity": 1, "selectable": true, "hasControls": true, "hasBorders": true, "hasRotatingPoint": true, "transparentCorners": true, "perPixelTargetFind": false, "text": "0001", "fontSize": 24, "fontWeight": 100, "fontFamily": "Arial", "fontStyle": "", "lineHeight": 1.3, "textDecoration": "", "textShadow": "", "textAlign": "left", "path": null, "strokeStyle": "", "backgroundColor": "", "useNative": true, "name": "text", "lockUniScaling": true, "config": { "feildId": "3", "feildName": "fldcname" }, "validations": { "maxwidth": 700, "maxheight": 400, "maxfont": 1000, "minfont": 0, "angle": 0 }, "controls": { "fontFamily": true, "fontSize": true, "boldFont": true, "normalFont": true, "italicFont": true, "colorFont": true, "groupOperations": true, "addToFront": true, "leftAlign": true, "rightAlign": true, "centerAlign": true, "underLine": true, "reAlign": true }, "originaltext": "0001", "meta": {} });
    alert(imgname);
    canvasdiscription[0].backgroundImage = imgname + "-2.jpg";
    alert(imgname + "-2.jpg");

    var canvasLimit = canvasdiscription.length;
    var canvasData = [];

    var jcrop_api;
    var bounds, boundx, boundy;
    var c = {};
    for (var i = 0; i < canvasdiscription.length; i++)
    {
        var canvas = {};
        canvas.json = canvasdiscription[i];
        alert(canvasdiscription.length);
        canvas.height = 559;
        canvas.width = 397;
        canvas.scaleFactorX = 1; // 0.75714285714286;
        canvas.scaleFactorY = 1; // 0.75714285714286;
        canvas.left = 10;
        canvas.top = 10;
        canvasData.push(canvas);
    }

    console.log(canvasData);

    Start(canvasLimit, canvasData);
    //Initially run the function:
    $(window).resize();
});
</script>

在此代碼中,我正在上載JPG圖像,但是畫布的大小是固定的,我想根據該上載的圖像調整畫布的大小。 當我將畫布調整為圖像的高度和寬度時,它無法正常工作。 這個你能幫我嗎。 我希望你們能解決這個問題。 謝謝!

我想我找到了回答您問題的方法。 正如我在評論中提到的那樣,在最后一個循環中有錯誤,我的代碼似乎與之一致:

<script type="text/javascript">
    var canvasdiscription = [{ "objects": [], "background": "rgba(0, 0, 0, 0)", "backgroundImage": "pug.jpg", "backgroundImageOpacity": 1, "backgroundImageStretch": true}];
    canvasdiscription[0].objects.push({ "type": "leg-text", "left": 202, "top": 51, "width": 231, "height": 31.2, "fill": "#000000", "overlayFill": null, "stroke": null, "strokeWidth": 1, "strokeDashArray": null, "scaleX": 1.53, "scaleY": 1.53, "angle": 0, "flipX": false, "flipY": false, "opacity": 1, "selectable": true, "hasControls": true, "hasBorders": true, "hasRotatingPoint": true, "transparentCorners": true, "perPixelTargetFind": false, "text": "0001", "fontSize": 24, "fontWeight": 100, "fontFamily": "Arial", "fontStyle": "", "lineHeight": 1.3, "textDecoration": "", "textShadow": "", "textAlign": "left", "path": null, "strokeStyle": "", "backgroundColor": "", "useNative": true, "name": "text", "lockUniScaling": true, "config": { "feildId": "3", "feildName": "fldcname" }, "validations": { "maxwidth": 700, "maxheight": 400, "maxfont": 1000, "minfont": 0, "angle": 0 }, "controls": { "fontFamily": true, "fontSize": true, "boldFont": true, "normalFont": true, "italicFont": true, "colorFont": true, "groupOperations": true, "addToFront": true, "leftAlign": true, "rightAlign": true, "centerAlign": true, "underLine": true, "reAlign": true }, "originaltext": "0001", "meta": {} });

    var canvasData = [];

    //Ilterate through the contained json objects in variable 'canvasdiscription'
    for (var i = 0; i < canvasdiscription.length; i++)
    {
        //Generate a plain Javascript object
        var objectProperties = eval(canvasdiscription[i]);

        //I suppose you want to take the background image as base for the new canvas size
        var imageDimensions = new Image()
        imageDimensions.src = canvasdiscription[i].backgroundImage;

        //Ilterate through the contained json objects in variable 'canvasdiscription.objects',
        //because the property 'objects' contains a list of data in json format
        for (var j = 0; j < objectProperties.objects.length; j++)
        {
            //In my example the following lines modifies the orginal values
            objectProperties.objects[j].height = imageDimensions.height;
            objectProperties.objects[j].width = imageDimensions.width; 
            objectProperties.objects[j].scaleFactorX = 1; // 0.75714285714286;
            objectProperties.objects[j].scaleFactorY = 1; // 0.75714285714286;
            objectProperties.objects[j].left = 10;
            objectProperties.objects[j].top = 10;

            //Store that current item into an array
            canvasData.push(objectProperties.objects[j]);
        }
    }

    //Example output
    console.log("height: " + canvasdiscription[0].objects[0].height);
    console.log("top: " + canvasdiscription[0].objects[0].top);
    console.log("scaleFactorX: " + canvasdiscription[0].objects[0].scaleFactorX);
</script>

希望它對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM