0

I am trying to resize my image with javascript.

First, I place the image on the canvas, so I can manipulate it.

I can make it smaller but the problem is I can't make it bigger with Scale transformation. To make it easy, I will insert my code here

<!DOCTYPE HTML>
<html>
<body>
    <canvas id='canvas1'></canvas>
    <hr>
    <button id='read'>READ IMAGE</button>
    <hr>
    Scale <input type='range' value='1' min='0.25' max='2' step='0.25' id='scale'>
   <br><button id='default2'>Default Scalling</button>
   <hr/>
</body>
<script src='imagine.js'></script>
<script>
    var canvas = document.getElementById('canvas1')
    var obj = new pc(canvas)
    obj.image2canvas("565043_553561101348179_1714194038_a.jpg")

    var tes = new Array()
    document.getElementById('read').addEventListener('click',function(){
        tes = obj.image2read()
    })
    document.getElementById('scale').addEventListener('change',function(){
        var scaleval = this.value
        var xpos = 0
        var ypos = 0
        var xnow = 0
        var ynow = 0
        var objW = obj.width
        var objH = obj.height
        tesbackup = new Array()
        for(var c=0; c<tes.length; c++){
            temp = new Array()
            for(var d=0; d<4; d++){
                temp.push(255)
            }
            tesbackup.push(temp)
        }

        for(var i=0; i<tes.length; i++){
           xpos = obj.i2x(i)
           ypos = obj.i2y(i)
           xnow = Math.round( (xpos)*(scaleval))
           ynow = Math.round( (ypos)*(scaleval))
           var idxnow = obj.xy2i(xnow,ynow)
           tesbackup[idxnow][0] = tes[i][0]
           tesbackup[idxnow][1] = tes[i][1]
           tesbackup[idxnow][2] = tes[i][2]
        }
        obj.array2canvas(tesbackup)
   })
</script>

And then for the script that used on that html file, is a js file named imagine.js.

imagine.js

    function info(text){
        console.info(text)
    }

    function pc(canvas){
        this.canvas = canvas
        this.context = this.canvas.getContext('2d')
        this.width = 0
        this.height = 0
        this.imgsrc = ""
        this.image2read = function(){
            this.originalLakeImageData = this.context.getImageData(0,0, this.width, this.height)
            this.resultArr = new Array()
            this.tempArr = new Array()
            this.tempCount = 0
            for(var i=0; i<this.originalLakeImageData.data.length; i++){
                this.tempCount++
                this.tempArr.push(this.originalLakeImageData.data[i])
                if(this.tempCount == 4){
                    this.resultArr.push(this.tempArr)
                    this.tempArr = []
                    this.tempCount = 0
                }
            }
            info('image2read Success ('+this.imgsrc+') : '+this.width+'x'+this.height)
            return this.resultArr
        }

        this.image2canvas = function(imgsrc){
            var imageObj = new Image()
            var parent = this
            imageObj.onload = function() {
                parent.canvas.width = imageObj.width
                parent.canvas.height = imageObj.height
                parent.context.drawImage(imageObj, 0, 0)
                parent.width = imageObj.width
                parent.height = imageObj.height
                info('image2canvas Success ('+imgsrc+')')
            }   
            imageObj.src = imgsrc
            this.imgsrc = imgsrc
        }

        this.array2canvas = function(arr){
            this.imageData = this.context.getImageData(0,0, this.width, this.height)
            if(this.imageData.data.length != arr.length*4){
                error("array2canvas Failed to Execute")
                return false
            }
            for(var i = 0; i < arr.length; i++){
                this.imageData.data[(i*4)] = arr[i][0]
                this.imageData.data[(i*4)+1] = arr[i][1]
                this.imageData.data[(i*4)+2] = arr[i][2]
                this.imageData.data[(i*4)+3] = arr[i][3]
            }
            this.context.clearRect(0, 0, this.width, this.height)
            this.context.putImageData(this.imageData, 0, 0)
            info('Array2Canvas Success ('+this.imgsrc+')')
        }

        this.i2x = function(i){
            return (i % this.width)
        }

        this.i2y = function(i){
            return ((i - (i % this.width))/ this.width)
        }

        this.xy2i = function(x,y){
            return (y * this.width) + (x)
        }

    }

I screenshot the output of the script, the image can be zoomed to a smaller image but can't be zoomed to a bigger image canvas

Original size of the image enter image description here

Image size after I change the value to smaller value

enter image description here

But when I try to scroll it to a bigger value, the canvas get bigger but the images is disappeared. And I get the error "Cannot set property '0' of undefined".

Image size when I change the value to bigger value enter image description here

Can someone point out what should I add or remove to get the image resize to bigger using this code??

Thanks in advance

Willze Fortner
  • 141
  • 2
  • 2
  • 12

0 Answers0