Canvas

博客分类: 江河计划

Canvas

算法

位1的个数

编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011
var hammingWeight = function(n) {
    return n.toString(2).replace(/[^1]/g,'').length
};

汉明距离

两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 x 和 y,计算它们之间的汉明距离。

注意: 0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

上面的箭头指出了对应二进制位不同的位置。
var hammingDistance = function(x, y) {
    return (x^y).toString(2).replace(/[^1]/g,'').length
};

canvas

drawImage

在画布上绘制图片,图像压缩,水印合成,图像的像素操作

context.drawImage(image, dx, dy);
context.drawImage(image, dx, dy, dWidth, dHeight);
context.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

ellipse

绘制椭圆

context.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);

fill

路径填充方法

context.fill();
context.fill(fillRule);
context.fill(path, fillRule);

fillRect

矩形填充

context.fillRect(x, y, width, height);

fillText

填充文字

context.fillText(text, x, y [, maxWidth]);

文字自动换行

CanvasRenderingContext2D.prototype.wrapText = function (text, x, y, maxWidth, lineHeight) {
    if (typeof text != 'string' || typeof x != 'number' || typeof y != 'number') {
        return;
    }
    
    var context = this;
    var canvas = context.canvas;
    
    if (typeof maxWidth == 'undefined') {
        maxWidth = (canvas && canvas.width) || 300;
    }
    if (typeof lineHeight == 'undefined') {
        lineHeight = (canvas && parseInt(window.getComputedStyle(canvas).lineHeight)) || parseInt(window.getComputedStyle(document.body).lineHeight);
    }
    
    // 字符分隔为数组
    var arrText = text.split('');
    var line = '';
    
    for (var n = 0; n < arrText.length; n++) {
        var testLine = line + arrText[n];
        var metrics = context.measureText(testLine);
        var testWidth = metrics.width;
        if (testWidth > maxWidth && n > 0) {
            context.fillText(line, x, y);
            line = arrText[n];
            y += lineHeight;
        } else {
            line = testLine;
        }
    }
    context.fillText(line, x, y);
};

getImageData

返回一个ImageData对象,其中包含Canvas画布部分或完整的像素点信息。

context.getImageData(sx, sy, sWidth, sHeight);

getLineDash

获取当前虚线的样式,就是一段实线一段空隙交替出现的条线,而这里的数字列表中的值表示的就是交替的实线和间隙的长度值。如果设置虚线时候的数字个数是奇数,这样数量就变成偶数。 例如,返回值是一个数组,则数组里面的数字会被复制和连接,数组里面的值都是数字,虚线设置为 [5,10,15] 将返回 [5,10,15,5,10,15] 。,称为数字列表。所谓虚线

context.getLineDash();

isPointInPath

用来检测某个点是否在当前路径中。beginPath 之后的才会被检测

context.isPointInPath(x, y);
context.isPointInPath(x, y, fillRule);

isPointInStroke

用来检测对应的点是否在描边路径上,则检测区域越大,描边越粗

context.isPointInStroke(x, y);
context.isPointInStroke(path, x, y);

lineTo

绘制直线以连接当前最后的子路径点和 lineTo 指定的店

context.lineTo(x, y);

measureText

测量文档的数据。返回文字的宽度

context.measureText(text)

moveTo

路径绘制的过程中将点移动

context.moveTo(x, y);

putImageData

将给定ImageData对象的数据绘制到位图上

context.putImageData(imagedata, dx, dy);
context.putImageData(imagedata, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight);

quadraticCurveTo

绘制二次贝塞尔曲线,第一个是控制点,第二个点是终点。

context.quadraticCurveTo(cpx, cpy, x, y);