(function(){ var watermark = function(self){ this.elem = self; }; watermark.prototype = { defaults : { path : '',//ˮӡͼƬ��ַ width : 30, //�м�� height : 30, //�м�� textRotate : -30 , //��ת�Ƕ� -90 ~ 90 opacity: 1,//͸���� type: 'Tile',//Tile Ϊƽ�� ����Ϊ�Զ���λ�� scaleVal: 1, //���ű��� margin: 5,//�Զ���λ��ʱ �߾� gravity:'ne',//�Զ���λ�� nw | n | ne | w | c | e | sw | s | se fullOverlay: false//�Զ���ʱ ����ˮӡͼƬ }, options : { canvas : [] }, init : function(options){ $.extend(this.options, this.defaults, options); var $body = $('body'); var settings = this.options; var canOne = this.__createCanvas($body), canAll = this.__createCanvas($body); settings.deg = settings.textRotate * Math.PI / 180; //���� this.__calcTextSize($body); var ctxOne = this.__setCanvasStyle(canOne, settings.canvasWidth, settings.canvasHeight); this.__drawImgOne(ctxOne); var ctx = this.__setCanvasStyle(canAll, settings.canvasMaxWidth, settings.canvasMaxHeight, true); this.__drawImgAll(ctx, canOne); var dataURL = canAll.toDataURL("image/png"); $(this.elem).css('backgroundImage', "url(" + dataURL + ")"); this.__destory(); try{if(settings.callback) settings.callback();}catch(e){} }, __createCanvas : function($container){ var canvas = document.createElement('canvas'); $container.append(canvas); this.options.canvas.push(canvas); return canvas; }, __calcTextSize : function($container){ var canvasWidth = 0, canvasHeight = 0, settings = this.options, tWidth = settings.imgObj.width, tHeight = settings.imgObj.height, absdeg = Math.abs(settings.deg); //�ж�ͼƬ�������� ע�� �����г������� /*if(tWidth > $(this.elem).width() || tHeight > $(this.elem).height()){ settings.scaleVal = Math.max(Math.ceil(tWidth / $(this.elem).width()), Math.ceil(tHeight / $(this.elem).height())) + 1; }*/ canvasWidth = (Math.sin(absdeg) * tHeight + Math.cos(absdeg) * tWidth) / settings.scaleVal; canvasHeight = (Math.sin(absdeg) * tWidth + Math.cos(absdeg) * tHeight) / settings.scaleVal; //�и� �и� canvasWidth += settings.type == 'Tile' ? settings.width : 0; canvasHeight += settings.type == 'Tile' ? settings.height : 0; // var xRepeatTimes = 0, canvasMaxWidth = 0, yRepeatTimes = 0, canvasMaxHeight = 0; xRepeatTimes = Math.ceil($(this.elem).width() / canvasWidth); canvasMaxWidth = settings.type == 'Tile' ? canvasWidth * xRepeatTimes : $(this.elem).width(); yRepeatTimes = Math.ceil($(this.elem).height() / canvasHeight); canvasMaxHeight = settings.type == 'Tile' ? canvasHeight * yRepeatTimes : $(this.elem).height(); // settings.xRepeatTimes = xRepeatTimes; settings.canvasMaxWidth = canvasMaxWidth; settings.yRepeatTimes = yRepeatTimes; settings.canvasMaxHeight = canvasMaxHeight; settings.absdeg = absdeg; settings.canvasWidth = canvasWidth; settings.canvasHeight = canvasHeight; }, __setCanvasStyle : function(canvas, width, height, nostyle){ canvas.width = width; canvas.height = height; canvas.style.display='none'; var ctx = canvas.getContext('2d'); var settings = this.options; if(!nostyle) { var nx = Math.sin(settings.absdeg) * settings.imgObj.height / settings.scaleVal; var ny = Math.sin(settings.absdeg) * settings.imgObj.width / settings.scaleVal; nx = settings.textRotate < 0 ? 0 : nx; ny = settings.textRotate > 0 ? 0 : ny; ctx.translate(nx, ny); ctx.rotate(settings.deg); //͸���� var op = settings.opacity; if (op > 0 && op < 1) { ctx.globalAlpha = op; } } return ctx; }, __drawImgOne: function(ctx){ var settings = this.options; ctx.drawImage(settings.imgObj, 0, 0, settings.imgObj.width / settings.scaleVal, settings.imgObj.height / settings.scaleVal); }, __drawImgAll: function(ctx, canvas){ var settings = this.options; if(settings.type == 'Tile') {//ƽ�� /*ctx.clearRect(0, 0, settings.canvasMaxWidth, settings.canvasMaxHeight); var pat = ctx.createPattern(canvas, 'repeat'); ctx.rect(0, 0, settings.canvasMaxWidth, settings.canvasMaxHeight); ctx.fillStyle = pat; ctx.fill();*/ for(var J = 0; J < settings.yRepeatTimes; J++){ for(var i = 0; i < settings.xRepeatTimes; i++){ ctx.drawImage(canvas, settings.canvasWidth * i, settings.canvasHeight * J); } } }else{ //�Զ���λ�� var w = settings.canvasMaxWidth, h = settings.canvasMaxHeight, wmW = settings.fullOverlay ? w : canvas.width, wmH = settings.fullOverlay ? h : canvas.height, pos = settings.margin || 0, gLeft = 0, gTop = 0; switch (settings.gravity) { // nw | n | ne | w | c | e | sw | s | se case 'nw': // ���� gLeft = pos; gTop = pos; break; case 'n': // �� gLeft = w / 2 - wmW / 2; gTop = pos; break; case 'ne': // ���� gLeft = w - wmW - pos; gTop = pos; break; case 'w': // �� gLeft = pos; gTop = h / 2 - wmH / 2; break; case 'c': // �� gLeft = w / 2 - wmW / 2; gTop = h / 2 - wmH / 2; break; case 'e': // �� gLeft = w - wmW - pos; gTop = h / 2 - wmH / 2; break; case 'sw': // ���� gLeft = pos; gTop = h - wmH - pos; break; case 's': // �� gLeft = w / 2 - wmW / 2; gTop = h - wmH - pos; break; default: // ���� gLeft = w - wmW - pos; gTop = h - wmH - pos; } ctx.drawImage(canvas, gLeft, gTop, wmW, wmH); } }, __destory : function(){ try{ $.each(this.options.canvas, function(i, canvas){ canvas.remove(); canvas = null; }); }catch(e){} } }; $.fn.watermark = function(options){ try{ var _this = this; if(options.path != ''){ var imgObj = new Image(); //imgObj.src = options.path + "?rnd=" + Math.random(); imgObj.src = options.path; imgObj.onload = function () { options.imgObj = imgObj; new watermark(_this).init(options); }; } }catch(e){} } })(jQuery);