让你快速了解cavans针对图像处理一些操作的demo代码

16 篇文章 0 订阅

 

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.js"
        integrity="sha256-B2A44YCanY9zaHL5nwcihGM3GiWpOE4md3COMTdAggM=" crossorigin="anonymous"></script>
    <meta charset="utf-8" />
    <script
        src="https://rawcdn.githack.com/fanyonglong/DxFrameworks/21a66621dd1652da71398f2168a27f0bc358060d/utility/exampleTool.js"></script>
    <style>
        canvas {
            border: solid 1px red;
        }
    </style>
</head>

<body>


    <script>

        var imageSrc = '../images/daitu.jpg';//'/assets/images/domokun.jpg'

        /*
        image
一个图像源, 可以是一个 <img>, SVG <image>, <video>, <canvas>, HTMLImageElement, SVGImageElement, HTMLVideoElement, HTMLCanvasElement, Blob, ImageData, ImageBitmap, 或 OffscreenCanvas 对象.    
        */
        if (!('createImageBitmap' in window)) {
            window.createImageBitmap = async function (data) {
                return new Promise((resolve, reject) => {
                    let dataURL;
                    if (data instanceof Blob) {
                        dataURL = URL.createObjectURL(data);
                    } else if (data instanceof ImageData) {
                        const canvas = document.createElement('canvas');
                        const ctx = canvas.getContext('2d');
                        canvas.width = data.width;
                        canvas.height = data.height;
                        ctx.putImageData(data, 0, 0);
                        dataURL = canvas.toDataURL();
                    } else {
                        throw new Error('createImageBitmap does not handle the provided image source type');
                    }
                    const img = document.createElement('img');
                    img.addEventListener('load', function () {
                        resolve(this);
                    });
                    img.src = dataURL;
                });
            };
        }
        function createCanvas(width = 800, height = 600) {
            return $('<canvas>').attr({
                width: width,
                height: height
            }).css({
                border: 'solid 1px #888'
            });
            //    return d3.create('canvas').style('border', 'solid 1px #888').attr('width', width).attr('height', height);
        }
        var exmapleInstance = ExampleFactory(function (example) {

            return function (page) {
                page.onChange('init', function () {

                });
                page.onChange('show', function () {

                });
                page.onChange('hide', function () {

                });
                page.onChange('refresh', function () {

                });

                page.callback(page);

            }

        });
        dat.controllers.Controller.prototype.setTitle = function (title) {
            this.__li.firstElementChild.firstElementChild.setAttribute('title', title)
            return this;
        }
        function imageLoad(src) {
            return new Promise(function (resolve) {
                var img = new Image();
                img.onload = function () {
                    resolve(img);
                };
                img.src = src;
            })
        }
        exmapleInstance.addExample('DrawImage平铺', function (page) {
            var cvs = createCanvas(800, 600).appendTo(page.el)[0];
            var ctx = cvs.getContext('2d');
            var w = 50, h = 50;
            var imgResource;
            //drawImage(image, x, y, width, height)
            //这个方法多了2个参数:width 和 height,这两个参数用来控制 当向canvas画入时应该缩放的大小
            imageLoad(imageSrc).then(function (img) {

                for (var r = 0; r < 3; r++) {
                    for (var c = 0; c < 5; c++) {
                        ctx.drawImage(img, c * w, r * h, w, h)
                    }
                }
            })
        })
        /*
        image
        一个图像源, 可以是一个 <img>, SVG <image>, <video>, <canvas>, HTMLImageElement, SVGImageElement, HTMLVideoElement, HTMLCanvasElement, Blob, ImageData, ImageBitmap, 或 OffscreenCanvas 对象.
        sx
        裁剪点x坐标.
        sy
        裁剪点y坐标.
        sw
        裁剪宽度,值可为负数.
        sh
        裁剪高度,值可为负数.
        options 可选
        为其设置选项的对象。可用的选项是:
        imageOrientation: 指示图像是按原样呈现还是垂直翻转.  none (默认不翻转) 或 flipY.
        premultiplyAlpha: 指示位图颜色通道由alpha通道预乘. 选择其一:none, premultiply, 或 default (默认).
        colorSpaceConversion: 指示图像是否使用色彩空间转换进行解码. none 或 default (默认). The value default indicates that implementation-specific behavior is used.
        resizeWidth: 指示新宽度的长整数。
        resizeHeight: 指示新高度的长整数。
        resizeQuality: 指定图像缩放算法. 选择其一pixelated, low (默认), medium, 或 high.
        */
        exmapleInstance.addExample('createImageBitmap', function (page) {
            var cvs = createCanvas(800, 600).appendTo(page.el)[0];
            var ctx = cvs.getContext('2d');
            var w = 50, h = 50;
            var imgResource;
            //drawImage(image, x, y, width, height)
            //这个方法多了2个参数:width 和 height,这两个参数用来控制 当向canvas画入时应该缩放的大小
            imageLoad(imageSrc).then(function (img) {
                //img.width=800;
                //img.height=600;
                createOptionsGui({
                    sx:0,
                    sy:0,
                    sw:[img.width,10,img.width],
                    sh:[img.height,10,img.height],
                    imageOrientation:{
                        type:'options',
                        value:"none",
                        options:['none','flipY']
                    },
                    resizeWidth:[100,0,800],
                    resizeHeight:[100,0,600]
                },page.operation,{
                    immediately:true
                })(function(n,v,ops){
                    ctx.clearRect(0,0,800,600);
                    var imagebitmap=createImageBitmap(img,ops.sx,ops.sy,ops.sw,ops.sh,{
                        imageOrientation:ops.imageOrientation,
                        resizeWidth:ops.resizeWidth,
                        resizeHeight:ops.resizeHeight
                    })
                    imagebitmap.then(function(imagebitmap){
                        ctx.drawImage(imagebitmap,0,0);
                    })
           
                })
            })
        })

        exmapleInstance.addExample('createPattern平铺', function (page) {
            var cvs = createCanvas(800, 600).appendTo(page.el)[0];
            var ctx = cvs.getContext('2d');
            var w = 50, h = 50;
            var imgResource;
            /**
             * image
             作为重复图像源的 CanvasImageSource 对象。可以是下列之一:
             HTMLImageElement (<img>),
             HTMLVideoElement (<video>),
             HTMLCanvasElement (<canvas>),
             CanvasRenderingContext2D,
             ImageBitmap,
             ImageData,
             Blob.
             repetition
             DOMString,指定如何重复图像。允许的值有:
             "repeat" (both directions),
             "repeat-x" (horizontal only),
             "repeat-y" (vertical only), 
             "no-repeat" (neither).
             如果为空字符串 ('') 或 null (但不是 undefined),repetition将被当作"repeat"。
            */
            imageLoad(imageSrc).then(function (img) {
                //img.width = w;
                //img.height = h;
                ctx.drawImage(img, 0, 0, 50, 50);
                var imgdata = ctx.getImageData(0, 0, 50, 50);
                /*
                options 可选
                为其设置选项的对象。可用的选项是:
                imageOrientation: 指示图像是按原样呈现还是垂直翻转.  none (默认不翻转) 或 flipY.
                premultiplyAlpha: 指示位图颜色通道由alpha通道预乘. 选择其一:none, premultiply, 或 default (默认).
                colorSpaceConversion: 指示图像是否使用色彩空间转换进行解码. none 或 default (默认). The value default indicates that implementation-specific behavior is used.
                resizeWidth: 指示新宽度的长整数。
                resizeHeight: 指示新高度的长整数。
                resizeQuality: 指定图像缩放算法. 选择其一pixelated, low (默认), medium, 或 high.
                */
             
                imgdata = createImageBitmap(img, 0, 0, img.width, img.height,{
                    resizeWidth:50,
                    resizeHeight:50
                });//ctx.createImageData(imgdata)
                imgdata.then(function (imageBitmap) {
                    createOptionsGui({
                        repeat: {
                            type: "options",
                            value: 'repeat',
                            options: ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
                        }
                    }, page.operation, {
                            immediately: true
                        })(function (n, v, ops) {
                            ctx.clearRect(0, 0, 800, 600);
                           // ctx.drawImage(imageBitmap,0,0)
                            var pattern = ctx.createPattern(imageBitmap, ops.repeat);
                            ctx.fillStyle = pattern;
                            ctx.fillRect(0,0,800,600);
                           // ctx.arc(800 / 2, 300, 200, 0, Math.PI * 2)
                            ctx.fill();

                        })
                });

            })
        })
        exmapleInstance.addExample('DrawImage(缩放 Scaling)', function (page) {
            var cvs = createCanvas(400, 400).appendTo(page.el)[0];
            var ctx = cvs.getContext('2d');
            var gui = page.operation;

            /*
            void ctx.drawImage(image, dx, dy);
            void ctx.drawImage(image, dx, dy, dWidth, dHeight);
            void ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
            sx可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的左上角 X 轴坐标。
            sy可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的左上角 Y 轴坐标。
            sWidth可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的宽度。如果不说明,整个矩形(裁剪)从坐标的sx和sy开始,到image的右下角结束。
            sHeight可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的高度。
            dx
            image的左上角在目标canvas上 X 轴坐标。
            dy
            image的左上角在目标canvas上 Y 轴坐标。
            dWidth可选
            image在目标canvas上绘制的宽度。 允许对绘制的image进行缩放。 如果不说明, 在绘制时image宽度不会缩放。
            dHeight可选
            image在目标canvas上绘制的高度。 允许对绘制的image进行缩放。 如果不说明, 在绘制时image高度不会缩放。
            */
            class DrawImage {
                constructor(object, gui) {
                    this.object = object;
                    this.dx = 0;
                    this.dy = 0;
                    this.dWidth = 400;
                    this.dHeight = 400;
                    gui = gui.addFolder('DrawImage');
                    gui.add(this, 'dx', 0, 400);
                    gui.add(this, 'dy', 0, 400);
                    gui.add(this, 'dWidth', 10, 400);
                    gui.add(this, 'dHeight', 10, 400);
                }
                drawImag(img) {
                    this.object.drawImage(img, this.dx, this.dy, this.dWidth, this.dHeight)
                }
            }
            class ImageDataGui {
                constructor(object, gui) {
                    this.object = object;
                    this.sx = 0;
                    this.sy = 0;
                    this.sw = 400;
                    this.sh = 400;

                    gui = gui.addFolder('getImageData')
                    // gui.add(this,'title').name('将要被提取的图像数据矩形区域的左上角 x 坐标')
                    gui.add(this, 'sx', 0, 400, 1).setTitle('将要被提取的图像数据矩形区域的左上角');

                    gui.add(this, 'sy', 0, 400, 1).setTitle('将要被提取的图像数据矩形区域的左上角 y 坐标')

                    gui.add(this, 'sw', 10, 400, 1).setTitle('将要被提取的图像数据矩形区域的宽度')
                    gui.add(this, 'sh', 10, 400, 1).setTitle('将要被提取的图像数据矩形区域的高度');
                }
                getImageData() {
                    return this.object.getImageData(this.sx, this.sy, this.sw, this.sh);
                }
            }
            class PutImageData {
                constructor(object, gui) {
                    this.object = object;
                    this.dx = 0;
                    this.dy = 0;
                    this.dirtyX = 0;
                    this.dirtyY = 0;
                    this.dirtyWidth = 400;
                    this.dirtyHeight = 400;
                    gui = gui.addFolder('putImageData');
                    gui.add(this, 'dx', 0, 400);
                    gui.add(this, 'dy', 0, 400);
                    gui.add(this, 'dirtyX', 0, 400);
                    gui.add(this, 'dirtyY', 0, 400);
                    gui.add(this, 'dirtyWidth', 10, 400);
                    gui.add(this, 'dirtyHeight', 10, 400);
                }
                putImageData(imagedata) {
                    this.object.putImageData(imagedata, this.dx, this.dy, this.dirtyX, this.dirtyY, this.dirtyWidth, this.dirtyHeight)
                }
            }
            var _DrawImage = new DrawImage(ctx, gui);
            var _ImageDataGui = new ImageDataGui(ctx, gui);

            var preview = createCanvas(400, 400), previewCtx = preview[0].getContext('2d');
            preview.appendTo(page.el)
            var putImageData = new PutImageData(previewCtx, gui)
            function render() {
                ctx.clearRect(0, 0, 400, 400);
                _DrawImage.drawImag(imgResource);
                var imageData = _ImageDataGui.getImageData();
                previewCtx.clearRect(0, 0, 400, 400);
                putImageData.putImageData(imageData);
                return true;
            }
            function init() {
                jQuery.fx.timer(render)

            }
            var imgResource;
            imageLoad(imageSrc).then(function (img) {
                page.el.appendChild(img);
                imgResource = img;
                init();
            })

        })
        exmapleInstance.addExample('DrawImage(切片)', function (page) {
            var cvs = createCanvas(400, 400).appendTo(page.el)[0];
            var ctx = cvs.getContext('2d');
            var gui = page.operation;

            /*
            void ctx.drawImage(image, dx, dy);
            void ctx.drawImage(image, dx, dy, dWidth, dHeight);
            void ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
            sx可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的左上角 X 轴坐标。
            sy可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的左上角 Y 轴坐标。
            sWidth可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的宽度。如果不说明,整个矩形(裁剪)从坐标的sx和sy开始,到image的右下角结束。
            sHeight可选
            需要绘制到目标上下文中的,image的矩形(裁剪)选择框的高度。
            dx
            image的左上角在目标canvas上 X 轴坐标。
            dy
            image的左上角在目标canvas上 Y 轴坐标。
            dWidth可选
            image在目标canvas上绘制的宽度。 允许对绘制的image进行缩放。 如果不说明, 在绘制时image宽度不会缩放。
            dHeight可选
            image在目标canvas上绘制的高度。 允许对绘制的image进行缩放。 如果不说明, 在绘制时image高度不会缩放。
            */
            class DrawImage {
                constructor(object, gui,img) {
                    this.object = object;
                    this.sx = 0;
                    this.sy = 0;
                    this.sWidth = img.width;
                    this.sHeight = img.height;
                    this.dx = 0;
                    this.dy = 0;
                    this.dWidth = 400;
                    this.dHeight = 400;
                    gui = gui.addFolder('DrawImage');
                    gui.add(this, 'sx', 0, 400);
                    gui.add(this, 'sy', 0, 400);
                    gui.add(this, 'sWidth', 10, img.width);
                    gui.add(this, 'sHeight', 10, img.height);
                    gui.add(this, 'dx', 0, 400);
                    gui.add(this, 'dy', 0, 400);
                    gui.add(this, 'dWidth', 10, 400);
                    gui.add(this, 'dHeight', 10, 400);
                }
                drawImag(img) {
                    this.object.drawImage(img, this.sx, this.sy, this.sWidth, this.sHeight, this.dx, this.dy, this.dWidth, this.dHeight)
                }
            }
            class ImageDataGui {
                constructor(object, gui) {
                    this.object = object;
                    this.sx = 0;
                    this.sy = 0;
                    this.sw = 400;
                    this.sh = 400;

                    gui = gui.addFolder('getImageData')
                    // gui.add(this,'title').name('将要被提取的图像数据矩形区域的左上角 x 坐标')
                    gui.add(this, 'sx', 0, 400, 1).setTitle('将要被提取的图像数据矩形区域的左上角');

                    gui.add(this, 'sy', 0, 400, 1).setTitle('将要被提取的图像数据矩形区域的左上角 y 坐标')

                    gui.add(this, 'sw', 10, 400, 1).setTitle('将要被提取的图像数据矩形区域的宽度')
                    gui.add(this, 'sh', 10, 400, 1).setTitle('将要被提取的图像数据矩形区域的高度');
                }
                getImageData() {
                    return this.object.getImageData(this.sx, this.sy, this.sw, this.sh);
                }
            }
            class PutImageData {
                constructor(object, gui) {
                    this.object = object;
                    this.dx = 0;
                    this.dy = 0;
                    this.dirtyX = 0;
                    this.dirtyY = 0;
                    this.dirtyWidth = 400;
                    this.dirtyHeight = 400;
                    gui = gui.addFolder('putImageData');
                    gui.add(this, 'dx', 0, 400);
                    gui.add(this, 'dy', 0, 400);
                    gui.add(this, 'dirtyX', 0, 400);
                    gui.add(this, 'dirtyY', 0, 400);
                    gui.add(this, 'dirtyWidth', 10, 400);
                    gui.add(this, 'dirtyHeight', 10, 400);
                }
                putImageData(imagedata) {
                    this.object.putImageData(imagedata, this.dx, this.dy, this.dirtyX, this.dirtyY, this.dirtyWidth, this.dirtyHeight)
                }
            }
            
            imageLoad(imageSrc).then(function (img) {
       
           
                    var _DrawImage = new DrawImage(ctx, gui,img);
                var _ImageDataGui = new ImageDataGui(ctx, gui);

                var preview = createCanvas(400, 400), previewCtx = preview[0].getContext('2d');
                preview.appendTo(page.el)
                var putImageData = new PutImageData(previewCtx, gui)
                function render() {
                    ctx.clearRect(0, 0, 400, 400);
                    _DrawImage.drawImag(img);
                    var imageData = _ImageDataGui.getImageData();
                    previewCtx.clearRect(0, 0, 400, 400);
                    putImageData.putImageData(imageData);
                    return true;
                }
                page.el.appendChild(img);
                jQuery.fx.timer(render)
            })

        })
    </script>
</body>

</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值