/**
* Created with JetBrains WebStorm.
* User: zhy
* Date: 13-12-17
* Time: 下午9:42
* To change this template use File | Settings | File Templates.
*/
functionCanvas2D($canvas)
{
varcontext = $canvas[0].getContext("2d"),
width = $canvas[0].width,
height = $canvas[0].height,
pageOffset = $canvas.offset();
context.font ="24px Verdana, Geneva, sans-serif";
context.textBaseline ="top";
/**
* 绘制矩形
* @param start
* @param end
* @param isFill
*/
this.drawRect =function(start, end, isFill)
{
varw = end.x - start.x , h = end.y - start.y;
if(isFill)
{
context.fillRect(start.x, start.y, w, h);
}
else
{
context.strokeRect(start.x, start.y, w, h);
}
};
/**
* 根据书写的文本,得到该文本在canvas上书写的中心位置的左上角坐标
* @param text
* @returns {{x: number, y: number}}
*/
this.caculateTextCenterPos =function(text)
{
varmetrics = context.measureText(text);
console.log(metrics);
// context.font = fontSize + "px Verdana, Geneva, sans-serif";
vartextWidth = metrics.width;
vartextHeight = parseInt(context.font);
return{
x: width / 2 - textWidth / 2,
y: height / 2 - textHeight / 2
};
}
this.width =function()
{
returnwidth;
}
this.height =function()
{
returnheight;
}
this.resetOffset =function()
{
pageOffset = $canvas.offset();
}
/**
* 当屏幕大小发生变化,重新计算offset
*/
$(window).resize(function()
{
pageOffset = $canvas.offset();
});
/**
* 将页面上的左边转化为canvas中的坐标
* @param pageX
* @param pageY
* @returns {{x: number, y: number}}
*/
this.getCanvasPoint =function(pageX, pageY)
{
return{
x: pageX - pageOffset.left,
y: pageY - pageOffset.top
}
}
/**
* 清除区域,此用户鼠标擦出刮奖涂层
* @param start
* @returns {*}
*/
this.clearRect =function(start)
{
context.clearRect(start.x, start.y, 10, 10);
returnthis;
};
/**
*将文本绘制到canvas的中间
* @param text
* @param fill
*/
this.drawTextInCenter =function(text, fill)
{
varpoint =this.caculateTextCenterPos(text);
if(fill)
{
context.fillText(text, point.x, point.y);
}
else
{
context.strokeText(text, point.x, point.y);
}
};
/**
* 设置画笔宽度
* @param newWidth
* @returns {*}
*/
this.penWidth =function(newWidth)
{
if(arguments.length)
{
context.lineWidth = newWidth;
returnthis;
}
returncontext.lineWidth;
};
/**
* 设置画笔颜色
* @param newColor
* @returns {*}
*/
this.penColor =function(newColor)
{
if(arguments.length)
{
context.strokeStyle = newColor;
context.fillStyle = newColor;
returnthis;
}
returncontext.strokeStyle;
};
/**
* @param fontSize
* @returns {*}
*/
this.fontSize =function(fontSize)
{
if(arguments.length)
{
context.font = fontSize +"px Verdana, Geneva, sans-serif";
returnthis;
}
returncontext.fontSize;
}
}