使用JavaScript的Canvas模拟CSS3中的background-size:cover;

语法

background-size: length|percentage|cover|contain;
描述 测试
length

设置背景图像的高度和宽度。

第一个值设置宽度,第二个值设置高度。

如果只设置一个值,则第二个值会被设置为 "auto"。

测试
percentage

以父元素的百分比来设置背景图像的宽度和高度。

第一个值设置宽度,第二个值设置高度。

如果只设置一个值,则第二个值会被设置为 "auto"。

测试
cover

把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。

背景图像的某些部分也许无法显示在背景定位区域中。

测试
contain 把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域。 测试
var image = new Image(),
	_this = this;
image.onload = function () {
	//_this.maskCtx.drawImage(this, 0, 0);
	var width = _this.width, // 绘制宽度,使水平方向全部覆盖
		height = this.height * width / this.width, // 绘制高度,高度等比缩放
		x = 0, // 水平开始坐标
		y = (_this.height - height) / 2; // 竖直开始坐标,垂直居中
	console.log(width, height, x, y);
	if(height < _this.height) { // 竖直方向不能完全覆盖
		height = _this.height; // 使竖直方向全部覆盖
		width = this.width * height / this.height; // 宽度等比缩放
		x = (_this.width - width) / 2; // 水平方向居中
		y = 0;
	}
	console.log(width, height, x, y);
	ctx.drawImage(this, 0, 0, this.width, this.height, x, y, width, height);
}
image.src = this.cover;


.signature-trace-drawer display: flex; -webkit-box-orient: vertical; -webkit-box-direction: reverse; -webkit-flex-direction: column-reverse; -moz-box-orient: vertical; -moz-box-direction: reverse; -ms-flex-direction: column-reverse; flex-direction: column-reverse; position: fixed; height: 100%; width: 100%; left: 0; top: 0; z-index: 1001; background: rgba(0, 0, 0, 0.5); justify-content: center align-items: center overflow: scroll .signature-trace-content width 100% justify-content: center align-items: center .signature-trace-wrapper-body position relative background: white margin-left 20px margin-right 20px padding 0 20px border-radius: 12px .signature-trace-wrapper-close position: absolute left 50% bottom -50px height 24px width 24px @extends button.close // background: url(../images/close_lt.png) no-repeat center / cover background-size: 20px background-color: white; opacity: 1 border-radius: 100% transform: translate(-50%, -50%) text-align: center color : rgb(166,166,166) span font-size 20px line-height 24px .signature-trace-title font-size: 17px font-weight bold padding-top: 15px margin-bottom: 10px text-align: center color #202020 .signature-trace-image-container background: url(&#39;../images/miziGrid.png&#39;) no-repeat center/cover; background-size: 100% 100% background-repeat: repeat; position relative aspect-ratio 1 border-radius: 12px; background-color: #F5F5F5; display: flex justify-content: center align-items: center overflow hidden .ps-sign position absolute height 100% min-width 100% aspect-ratio 1 z-index 1002 .canvas height 100% width 100% .signature-trace-spinner display: none width 24px height 24px position: absolute top: 50% left 50% background: url(&#39;../images/loading.gif&#39;) no-repeat center/cover; transform translate(-50%, -50%) .signature-trace-template-img display: none; width 100% opacity: 0.4; background: no-repeat center/cover; .signature-trace-template-text display: none; justify-content: center align-items: center width: calc(100% - 50px); height: calc(100% - 50px) position: absolute top: 45% left 50% transform: translate(-50%, -50%) opacity: 0.4; .signature-trace-reload-content display: flex align-items: center justify-content: center position: absolute top: 50% left 50% transform: translate(-50%, -50%) .signature-trace-reload font-size 14px color #808080 white-space: nowrap; span color #808080 .signature-trace-button display: flex; justify-content: space-between; align-items: center; margin: 20px 20px 0 padding-bottom: 20px .signature-trace-resetting font-size 14px flex-grow:1; width 100px height 36px line-height 36px text-align: center background: #F5F5F5; margin-right: 10px border-radius: 16px color #202020 opacity: 0.3; .signature-trace-determine font-size 14px background-color: #ED5151 flex-grow:1; height 36px; width 100px line-height 36px text-align: center color #FFFFFF border-radius: 16px opacity: 0.3; .signature-trace-determine.center-block height 35px width 182px flex-grow 0 position relative left 50% transform translateX(-50%) @media only screen and (min-device-aspect-ratio:3/4) .signature-trace-drawer .signature-trace-content .signature-trace-wrapper-body margin-left 60px margin-right 60px .signature-trace-title font-size 12px 如上的样式,类名signature-trace-wrapper-body的尺寸是401*401 请问他的尺寸是哪里来的呢
05-28
<template> <view class="bg-content"> <z-paging ref="paging1" v-model="dataList" @query="queryList"> <view slot="top"> <view class="tablesChange"> <view class="tables"> <u-tabs :list="tabsList4" @click="clickTabFn" :scrollable="true" lineColor="#FF6400" :activeStyle="{color: &#39;#303133&#39;,fontWeight: 600,transform: &#39;scale(1.05)&#39;,fontSize: &#39;31rpx&#39;}" :inactiveStyle="{color: &#39;#606266&#39;,fontWeight:500,transform: &#39;scale(1)&#39;,fontSize: &#39;31rpx&#39;}" itemStyle="padding-left: 30rpx; padding-right: 30rpx; height: 80rpx;"> </u-tabs> </view> </view> </view> <!-- <view class="my-evaluate f-jcsb f-a-i" @click="navMyevaluateFn" v-if="actTab==3"> <view class="f-a-i"> <u-icon type="eye" size="20" color="#fff"></u-icon> <u-icon name="chat" color="#999" size="18"></u-icon> 我的评价 </view> <view> <u-icon name="arrow-right" color="#999" size="18"></u-icon> </view> </view> --> <view> <view @click="jump()" class="list-cont" v-for="(item,index) in dataList" :key="index" > <canvas style="width:220rpx;height:86rpx;" canvas-id="colorCanvas"></canvas> <view class="top-flex"> <text class="grayfont">2025-7-10 11:29:15</text> <text style="font-weight: 500;">待支付</text> </view> <view class="main"> <image src="/static/images/tabbar/showroom_active.png" mode=""></image> <view class="main-msg"> <text class="font">东风风行E702022款500超享版</text> <view class="car-money"> <text class="blue-font">¥<text style="padding-right: 10rpx;">210000</text>元</text> </view> <view class="profit-estimate"> <text class="profit-label">预估可赚</text> <text class="profit-value">30000元</text> </view> </view> </view> <view class="main-bto"> <text>车辆年限:1年内</text> <text>公里数:1.78万公里</text> <text>使用性质:非营运</text> </view> </view> </view> </z-paging> </view> </template> <script> import { tabsList4 } from "@/data/tabsData2.js" export default { data() { return { tabsList4, actTab: -1, dataList: [], buttStyle: { width: &#39;160rpx&#39;, height: &#39;56rpx&#39;, marginLeft: &#39;16rpx&#39; }, imageUrl: &#39;https://cbl.diyouzhijia.com/static/images/ygctzmp/home/backgroundHeader.png&#39;, dominantColor: &#39;&#39;, topUpTypeShow: false, topUpType: &#39;ye&#39;, payPrice: 0, payData: { from: "", orderNo: "", payChannel: "yue", payType: "yue", scene: 0 } }; }, onLoad(){ this.getImageColor() }, methods: { // 加载图片时获取背景? async getImageColor() { try { // 1. 确保使用正确的Canvas上下文 const canvasId = &#39;colorCanvas&#39;; const ctx = uni.createCanvasContext(canvasId, this); // 2. 等待图片加载完成 await new Promise((resolve, reject) => { const img = new Image(); img.onload = () => resolve(img); img.onerror = reject; img.src = this.imageUrl; }); // 3. 绘制图片(使用明确尺寸) ctx.drawImage(this.imageUrl, 0, 0, 100, 100); // 4. 必须使用 draw(true) 同步绘制 await new Promise(resolve => ctx.draw(true, resolve)); // 5. 获取像素数据 const res = await new Promise((resolve, reject) => { uni.canvasGetImageData({ canvasId, x: 0, y: 0, width: 1, height: 1, success: resolve, fail: reject }); }); // 6. 提取颜色(注意微信小程序返回的data是Uint8ClampedArray) const pixelData = res.data || []; const [r, g, b] = pixelData.slice(0, 3); this.dominantColor = `rgb(${r}, ${g}, ${b})`; console.log(&#39;成功获取主色调:&#39;, this.dominantColor); } catch (err) { console.error(&#39;颜色分析失败:&#39;, err); this.dominantColor = &#39;rgb(255,255,255)&#39;; // 默认白色 } }, jump(){ uni.navigateTo({ url:&#39;/pagesB/home/publish&#39; }) }, topUpTypeClose() { this.topUpTypeShow = false }, // 去支付 //详情 navParticularFn(item) { uni.navigateTo({ url:&#39;/pagesA/showroom/newPayOrder/detalorder?orderId=&#39;+item.id }) }, clickTabFn(tab) { this.actTab = tab.value this.$refs.paging1.refresh(); }, //订单列表 queryList(pageNo, pageSize) { // 此处请求仅为演示,请替换为自己项目中的请求 let obj = { page: pageNo, limit: pageSize, type: this.actTab } let arr=[ { name:&#39;1&#39;, value:&#39;2&#39; }, { name:&#39;2&#39;, value:&#39;3&#39; } ] this.$refs.paging1.complete(arr); // getInformationOrderList(obj).then(res => { // this.$refs.paging.complete(res.data.list); // }) }, }, onShow() { if (this.$refs.paging1) { this.$refs.paging1.refresh(); } } } </script> <style lang="scss" scoped> .bg-content { min-height: 100vh; background: #F6F7FB; } .order-list-butt { width: 100%; padding: 0 24rpx; .order-list-butt-go { width: 120rpx; font-size: 24rpx; font-weight: 400; color: #999999; image { width: 48rpx; height: 48rpx; } } .order-list-butt-refund { padding: 16rpx 24rpx; font-size: 24rpx; font-weight: 400; color: #111111; background: #F5F5F5; margin-top: 16rpx; } } .user-data { padding: 24rpx 24rpx 0 24rpx; .user-data-image { width: 48rpx; height: 48rpx; border-radius: 50%; background: #f5f5f5; image { width: 100%; height: 100%; } } .user-data-name { font-size: 28rpx; font-weight: 400; color: #000000; margin-left: 16rpx; } .user-data-state { font-size: 24rpx; font-weight: bold; color: #FF6400; } } .car-rental { width: calc(100% - 48rpx); margin: 24rpx 24rpx 0 24rpx; padding-bottom: 24rpx; border-bottom: 1rpx solid #eee; .car-rental-cont { height: 150rpx; width: calc(100% - 218rpx); margin-left: 16rpx; display: flex; flex-direction: column; justify-content: space-around; .car-rental-cont-name { font-size: 32rpx; font-weight: 400; color: #111111; } .car-rental-cont-price { font-size: 32rpx; font-weight: bold; color: #111111; } .car-rental-cont-total-payment { font-size: 24rpx; font-weight: 400; color: #666666; } } image { width: 202rpx; height: 150rpx; } } .list-cont { // width: calc(100% - 30rpx); margin: 24rpx; background: #fff; border-radius: 8rpx; padding-bottom: 24rpx; // border: 1px solid red; padding: 30rpx; .top-flex{ display: flex; width: 100%; padding-bottom: 26rpx; justify-content: space-between; align-items: center; border-bottom: 1px solid #DBDBDB; text{ font-size: 28rpx; color: #2F74FA; font-weight: 500; } .grayfont{ color: #666666; font-size: 28rpx; } } .main-bto{ display: flex; width: 100%; align-items: center; justify-content: space-between; color: #666666; font-size: 24rpx; text{ padding-top: 30rpx; } } .main{ display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #DBDBDB; padding-bottom: 30rpx; margin-top: 30rpx; image{ width: 259rpx; height: 180rpx; border-radius: 10rpx; } .main-msg{ flex: 1; height: 100%; margin-left: 30rpx; display: flex; flex-direction: column; justify-content: space-between; .car-money{ margin-top: 10rpx; .blue-font{ color: #FF6400; font-size: 24rpx; text{ font-size: 36rpx; } } } // .bg-money{ // width: 100%; // height: 44rpx; // margin-top: 10rpx; // font-size: 24rpx; // font-weight: 500; // color: white; // line-height: 44rpx; // // border: 1px solid #3E80F5; // background: url(&#39;https://cbl.diyouzhijia.com/static/images/ygcmp/home/yugu.png&#39;) no-repeat; // background-size: 100% 100%; // } .profit-estimate { display: flex; align-items: center; width: 100%; height: 48rpx; margin-top: 10rpx; // margin-bottom: 30rpx; color: white; background: url(&#39;https://cbl.diyouzhijia.com/static/images/ygcmp/home/yugu.png&#39;) no-repeat; background-size: 100% 100%; .profit-label { color: #ffffff; font-weight: 700; font-size: 24rpx; margin: 0 31rpx 0 13rpx; } .profit-value { flex: 1; text-align: center; font-weight: 700; color: #2f74fa; font-size: 32rpx; } } .font{ color: #333333; font-size: 28rpx; // margin-top: 10rpx; font-weight: 500; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; /* 限制显示的行数 */ overflow: hidden; text-overflow: ellipsis; } } } } .tab-list { background: #fff; overflow: auto; view { width: 88rpx; padding-bottom: 12rpx; border-bottom: 6rpx solid transparent; font-size: 29rpx; font-weight: bold; display: inline-block; color: #555555; text-align: center; } .act-tab { border-bottom: 6rpx solid #000; font-size: 29rpx; font-weight: bold; color: #222222; } } .act-flaf { width: calc(100% - 56rpx); margin-left: 24rpx; } .circle { width: 30rpx; height: 30rpx; border-radius: 50%; border: 2rpx solid #D7D7D7; } .my-evaluate { background: #fff; padding: 16rpx 24rpx; width: calc(100% - 48rpx); margin: 24rpx; border-radius: 8rpx; } .tablesChange{ // padding: 0px 30rpx; .tables{ background-color: white; // border-radius: ; border-radius: 20rpx 20rpx 0px 0px; padding: 30rpx 20rpx; padding-bottom: 0px; } } </style>上面代码中颜色分析失败: ReferenceError: Image is not defined报错;帮我写出完整可以用的getImageColor函数里的方法
最新发布
07-24
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>原神登录注册表单</title> <style> body { font-family: &#39;Arial&#39;, sans-serif; background: url(&#39;https://i.imgur.com/3QZQZ9m.jpg&#39;) no-repeat center center fixed; background-size: cover; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } .form-container { background: rgba(0, 0, 0, 0.7); padding: 30px; border-radius: 10px; box-shadow: 0 0 20px rgba(255, 255, 255, 0.2); width: 350px; text-align: center; color: white; } .form-container h2 { font-size: 24px; margin-bottom: 20px; color: #ffcc00; } .form-container input { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ccc; border-radius: 5px; background: rgba(255, 255, 255, 0.1); color: white; font-size: 16px; } .form-container input::placeholder { color: #ccc; } .form-container button { width: 100%; padding: 10px; background: #ffcc00; color: #000; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; font-weight: bold; margin-top: 10px; } .form-container button:hover { background: #e6b800; } .toggle-button { background: none; border: none; color: #ffcc00; cursor: pointer; text-decoration: underline; font-size: 14px; } .toggle-button:hover { color: #e6b800; } .hidden { display: none; } </style> </head> <body> <div class="form-container"> <!-- 登录表单 --> <div id="login-form"> <h2>欢迎登录提瓦特大陆</h2> <input type="text" id="login-username" placeholder="用户名" required> <input type="password" id="login-password" placeholder="密码" required> <button type="submit" onclick="login()">登录</button> <p>没有账号?<button class="toggle-button" onclick="toggleForm(&#39;register-form&#39;, &#39;login-form&#39;)">注册</button></p> </div> <!-- 注册表单 --> <div id="register-form" class="hidden"> <h2>注册提瓦特大陆</h2> <input type="text" id="register-username" placeholder="用户名" required> <input type="password" id="register-password" placeholder="密码" required> <input type="email" id="register-email" placeholder="邮箱" required> <button type="submit" onclick="register()">注册</button> <p>已有账号?<button class="toggle-button" onclick="toggleForm(&#39;login-form&#39;, &#39;register-form&#39;)">登录</button></p> </div> </div> <script> // 切换表单显示 function toggleForm(showId, hideId) { document.getElementById(showId).classList.remove(&#39;hidden&#39;); document.getElementById(hideId).classList.add(&#39;hidden&#39;); } // 登录功能 function login() { const username = document.getElementById(&#39;login-username&#39;).value; const password = document.getElementById(&#39;login-password&#39;).value; alert(`欢迎回到提瓦特大陆,${username}!\n登录成功!`); } // 注册功能 function register() { const username = document.getElementById(&#39;register-username&#39;).value; const password = document.getElementById(&#39;register-password&#39;).value; const email = document.getElementById(&#39;register-email&#39;).value; alert(`欢迎加入提瓦特大陆,${username}!\n注册成功!`); } </script> </body> </html> 创建一个首页页面
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值